Forums > MaxMSP

Random Range



pmj
Jul 08 2009 | 5:23 pm

I’m sure that this must be within the tutorials somewhere, but I can’t seem to locate it.

Using the [random] object, how do you set it so that only a number within a specific range is output post-bang?

The tutorial and helpfile covers an output between 0 and an upper limit number, but what if I wanted to output any number with a minimum higher than 0, between 100 and 500 for example?

Jul 08 2009 | 5:27 pm

Have it do a random output between 0 and 400, then add 100 to the output.

Jul 08 2009 | 5:28 pm

You’d use the range as the argument to [random] in this case it is 400. Then you’d create the offset by connecting a [+ 100] after it. Voila, random numbers between 100 and 500. Each object performs a certain task and it is up to you to connect them together to reach your objective. You can also use [expr] to generate random numbers within certain bounds.

lh

Jul 08 2009 | 5:31 pm

An alternative way is [expr random(100,500)].

_
johan

Jul 08 2009 | 5:35 pm

Out of curiosity, is one method preferable over the other?


pmj
Jul 08 2009 | 5:41 pm

Using [expr random(100,500)] seems a more elegant way of doing it than adding 100 as an offset, but I don’t suppose it makes a great deal of difference.

Thanks very much everyone, very helpful as always.

Jul 08 2009 | 5:48 pm
pmj wrote on Wed, 08 July 2009 10:41
Using [expr random(100,500)] seems a more elegant way of doing it than adding 100 as an offset, but I don’t suppose it makes a great deal of difference.

Thanks very much everyone, very helpful as always.

Yes, but I’ve always heard that simple math objects are preferred over expr objects for efficiency. I’m curious how using [random 400]–>[+ 100] compares to [expr random(100,500)] in efficiency.

Jul 08 2009 | 6:28 pm

Note that expr is limited to 16 bits. Furthermore, don’t forget that the maximum is not included. So if you really want numbers from 100 to 500 included, random 401 and + 100 (or expr "random(100, 401)" will do the trick.

Jul 09 2009 | 3:41 pm

Probably the easiest (and most efficient) way to do what you want is with any of the basic random number generators in the Litter Power Package.

For instance, [lp.titi 100 500] will generate random numbers in the range [100 500]. Inclusive.

One object, no adding, no expr, and no fiddling around with the range not doing what you want.

As ej hints, the most common error with the standard random object is people almost always set the range one unit smaller than they really want.

Lp.titi is available in the Litter Starter Pack (URI below). The Starter Pack is more Max-4 friendly, but will run on Max 5, despite some some cosmetic error messages. The full Litter Pro bundle is more conscientiously Max5-friendly.

Viewing 9 posts - 1 through 9 (of 9 total)

Forums > MaxMSP