Random Range

pmj's icon

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?

bkshepard's icon

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

Luke Hall's icon

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

jvkr's icon

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

_
johan

bkshepard's icon

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

pmj's icon

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.

bkshepard's icon

pmj wrote on Wed, 08 July 2009 10:41Using [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.

Emmanuel Jourdan's icon

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.

Peter Castine's icon

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.