Reversing elements of a list if $i1 > $i2

Mikelib's icon

Hello,
I am using uzi and random to generate a list of two numbers. The two numbers are then fed into an sfplay~ object as a seek message to play a section of a sample. The problem I am having is that when the list is generated the first number can often be larger than the second. I need to find a way to reverse the numbers if the first is larger than second and combine in a new list. How can I do this? I have tried using if statement but cant work how to do this.

Thanks in advance. The patch is below.

Mike.

Max Patch
Copy patch and select New From Clipboard in Max.

Jean-Francois Charles's icon

[zl sort]

By the way, in your patch, [collect] and [show] are not core Max objects. You could streamline your patch, and use only Max objects for what you are doing.
Also, you don't want to use things like [delay 5] where a [trigger] could do the trick. Always use [trigger] (or [b]) when you can.
Best,
Jean-François.

Bas van der Graaff's icon

[if $f1 > $f2 then $f2 $f1 else $f1 $f2] is also fine by me.

seejayjames's icon

Or you can choose two random numbers, one for the start point, then the other one is the length---just add it to the first one, so it will always be larger. This would give more control (even in a random sense) over the eventual ranges you generate.

Or for a slightly different take, choose a random start point and add a given amount to it for the second: the second # is the duration. This of course can be altered on the fly as desired, tied to a MIDI control, etc.

Jean-Francois Charles's icon

Or something like that, just to make sure the right-to-left order is well understood.

Max Patch
Copy patch and select New From Clipboard in Max.

Mikelib's icon

Hi,
Thanks for all the replies and examples. Much appreciated. [zl sort] looks especially useful. I have streamlined the patch using only core max objects and this is what I have come up with. comments would be appreciated.

Thanks again, Mike.

Max Patch
Copy patch and select New From Clipboard in Max.

Jean-Francois Charles's icon

If you don't need the message boxes, don't use them.
When you need to display numbers to debug, etc, use additional [flonum] objects, or message boxes with left input, but don't put them in your data flow if they're not functional.

Jean-François.

Mikelib's icon

Excellent. Thanks again Jean-Francois for all the help.

Mike.

Luke Hall's icon

The point Jean-Francois makes is a good one. Try something a bit like the patch below. You could also use [sfinfo~] to find out the length of the loaded file and use that when you generate the numbers so any portion of the file could be selected.

lh

Max Patch
Copy patch and select New From Clipboard in Max.

Mikelib's icon

Thanks for the ideas. Sfinfo~ will be very handy!

Mike.

Peter Castine's icon

seejayjames wrote on Tue, 23 June 2009 17:39Or you can choose two random numbers, one for the start point, then the other one is the length---just add it to the first one, so it will always be larger. This would give more control (even in a random sense) over the eventual ranges you generate.

Better still, since we perceive durations and rhythms logarithmically, you could consider having exponentially distributed values for the duration.

If you want to do the math and stick to factory objects, you can. But most people find it *much* easier to generate an exponential or log-normal distribution using lp.expo and lp.lonnie (respectively).

Both objects are in the Litter Pro Bundle. I'm afraid there isn't anything out of the box in the Starter Pack that will do all the heavy lifting for you.