## random probability

Mar 8, 2010 at 9:28am

# random probability

Hi guys, I’d like using “random”, with a range for example of 10, so that in a 80% it produces number 2. how can i do it?

#49001
Mar 8, 2010 at 10:48am

The solution depends on what kind of distribution you want of course, but a simple way in Max would be something like this:

– Pasted Max Patch, click to expand. –

I.e. you start with a list containing all possible numbers, with number repeated more often if you want them to appear more often. For example, if you filled the list with 100 numbers, 80 of them being a “2″, then you’d get a “2″ 80% of the times of banging that list.

But here’s a somewhat more elegant method in javascript:

function bang()
{
var weights = [0.005, 0.05, 0.8, 0.05, 0.02, 0.02, 0.02, 0.02, 0.01, 0.005];
var weightsum = 0;
var randvar = Math.random(); // A random number between 0 and 1

for (i = 0; iweightsum && randvar

#176072
Mar 8, 2010 at 10:57am

And all the other numbers together make up the other 20%, evenly divided?

– Pasted Max Patch, click to expand. –
#176073
Mar 8, 2010 at 11:40am

table / itable is the way to go…

say you want 0, 1, 2 to be randomly chosen 10%, 60% and 30% of the times, respectively.
just create a table with size 3 and range 100 (this is just for ease of reading, in fact you can scale the probability as you like), and fill it with 0 10, 1 60, 2 30.
now start banging the table object, and it will yield you the random values with a probability weighted as you need it.
of course, it works with itable too, and you can graphically edit your weights.

aa

#176074
Mar 8, 2010 at 12:59pm

thanks everybody!!

#176075
Mar 8, 2010 at 5:21pm

For this particular case, the following is probably the tightest solution:

– Pasted Max Patch, click to expand. –

As always, if you’re serious about random numbers, you should take a look at Litter Power.

#176076
Mar 8, 2010 at 5:25pm

PS: The Tutorials in Max4 and earlier had an exercise for building a general “bang here w/prob x%”. Not sure where/if they are in Max 5 toots, but worth getting your hands on if you want some examples of how to be more flexible with the factory random objects.

#176077

You must be logged in to reply to this topic.