Forums > MaxMSP

### random probability

Mar 08 2010 | 9:28 am

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?

Mar 08 2010 | 10:48 am

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
```
Mar 08 2010 | 10:57 am

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

``````
-- Pasted Max  Patch, click to expand. --

```
```
Mar 08 2010 | 11:40 am

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

Mar 08 2010 | 12:59 pm

thanks everybody!!

Mar 08 2010 | 5:21 pm

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.
```
Mar 08 2010 | 5:25 pm

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.

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

Forums > MaxMSP