We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › how to increase probability with random values
Page Index Toggle Pages: 1
how to increase probability with random values? (Read 1628 times)
how to increase probability with random values?
Dec 10th, 2009, 8:08am
 
I'm guessing this isn't too difficult, I just don't know the proper way to go about doing it or what it is called. What I want to do is take a set of random values and then I want to increase/decrease the likelihood of certain values/ranges.

Here is an example...a point is drawn on the stage from 0 to 300 on the y-axis. Instead of randomly picking a point, I've determined that I prefer the end result of said program when this initial point is between 100 - 200. How would I go about increasing the likelihood of said values? Is there a name for this?

I theorized a way of doing this a long time ago(when I was first learning to program) where I had an array of values and I would just add more of the same values that I wanted to be more likely to happen into the array...but there has to be a better way. Is there a way to do something like have a 2 dimensional array and the second dimension has a "likelihood" value?

thanks in advance for any and all help!
Re: how to increase probability with random values?
Reply #1 - Dec 14th, 2009, 1:09am
 
something like have a 2 dimensional array

if i had to deal with tha issue, i believe that's the way i should do it.

int mylist[][] = new mylist[301][2]

first column to the real values
second column to the amount of times they apear.

when randomizing, instead of radomize betwen 0 and 300, randomize betwen 0 and the sum of all values in second column.

and yes, theres a name to that, i believe that concerns statistics from math, but i don't know how to translate to english.

it's rather dificult to me to write so tecnical already.

Code:

int mylist[][] = new mylist[6][1]

mylist[0][0] = 3;
mylist[1][0] = 5;
mylist[2][0] = 7;
mylist[3][0] = 9;
mylist[4][0] = 11;
mylist[5][0] = 13;

// this way they have all the same chance.
mylist[0][1] = 1;
mylist[1][1] = 1;
mylist[2][1] = 1;
mylist[3][1] = 1;
mylist[4][1] = 1;
mylist[5][1] = 1;

// but this way 9 has a bigger change
mylist[0][1] = 1;
mylist[1][1] = 1;
mylist[2][1] = 1;
mylist[3][1] = 5;
mylist[4][1] = 1;
mylist[5][1] = 1;

int function selectnumber()
{
    int i;
    int total;
    int r;

    for(i = 0; i<6;i++)
    {
   total+=mylist[i][1];
    }
   
    r = random(total);

    total = 0;
    for(i = 0; i<6;i++)
    {
   total+=mylist[i][1];
   if(total==r || total>r)
   {
  return mylist[i][1];
   }
    }
}



didn't tested the code.
Re: how to increase probability with random values?
Reply #2 - Dec 14th, 2009, 3:02am
 
what i made when i wanted to pick some numbers but wanted the higher ones to appear more often, was to add some numbers to an array and pick them by random

int[] numbers = { 1, 2, 3,4,5,6,6,7,7,8,8,9,9 };
randomNum = numbers[(int)random(numbers.length)];

but sure, this only works if you want to pick a out of a small number and you only get ints, so this is really limited. but i used it to pick some colors out of an array where i wanted the darker colors to appear more often, this was a nice way to adjust it.

Re: how to increase probability with random values?
Reply #3 - Dec 14th, 2009, 6:31am
 
Hi
Following on from Cedric's answer ...
if you want values between 100 and 200 to be twice as likely, you could find a number at random between 0 and 400, and if the number found is over 300, subtract 200 from it.

If you want a bell-shaped-ish curve of probability, then you could use some maths ...
Have a look at the first 12 squares, those up to 150 (half way between your prefered numbers 100 and 200):
1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144
and see how they are weighted - there're seven below 50.
Taking each of these from 150, and reversing the order we get:
6, 29, 50, 69, 86, 101, 114, 125, 134, 141, 146, 149
So, you could find a number at random up to square root 150 then fip a coin to subtract that from or add it to 150.
You'd get a more pronounced curve if you raise to a higher power.

string
Re: how to increase probability with random values?
Reply #4 - Dec 16th, 2009, 2:04pm
 
Off the top of my head, and maybe I'm missing something, but wouldn't it be better to express the values as probabilities and then convert them to a Cumulative Distribution Function?

E.g. I have a set of 4 numbers: {1, 10, 4, 3} and I want it to pick the second index 2.5x as often as the third index, the 3rd index 33% more often than the 4th index, etc.

Convert the numbers to probabilities by normalizing them. Sum the numbers, divide each number by the total. The sum is 18, the probabilities are {1/18, 10/18, 4/18, 3/18}.

Convert the probabilities set to a CDF. The value at each index is equal to the sum of all the values at the preceding indices. The range becomes: {1/18, 11/18, 15/18, 18/18}.

Then each time I need a sample I pick a random number between [0,1] and then starting at the beginning of the CDF, advances through the indices until I find a value >= my random number.
Page Index Toggle Pages: 1