Results 1 to 3 of 3
 03252011, 01:42 PM #1Member
 Join Date
 Mar 2011
 Posts
 9
 Rep Power
 0
Generate weighted random numbers, nextGaussian ()
I would like to randomly pick an element from an array. I can create a random number in a range: Min + (int)(Math.random() * ((Max  Min) + 1)); but I want to weight the random numbers to have more lower numbers.
The random function nextGaussian() generates a random number with normal distribution (which is what I want), but the upper limit isn't one. When I ran some tests, I got numbers as high as 2.1. Theoretically, there isn't an upper limit. How can I use this to generate an int for the array? I think I can use some If statements, but that seems both clunky and non random. here is my code:
Random randomnumber = new Random();
return (int)(Math.abs((randomnumber.nextGaussian()* (double)arraylength)));
where arraylength is the number of elements in my array.
I used abs since nextGaussian gives me a both neg and pos numbers.
 03252011, 05:49 PM #2Senior Member
 Join Date
 Aug 2010
 Posts
 127
 Rep Power
 0
Since you are working with discrete values (every position in the array is a natural number), you could just assign probabilities to each position.
For example::
position 1: odds of getting it should be 0.05
position 2: odds of getting it should be 0.04
....
...
put al those odds in an array (say "oddsValues") and then do something like this:
Java Code:Random randomnumber = new Random(); double randomChoice=randomnumber.getNextDouble(); int counter=0; double oddsTraversed=oddsValues[0]; while(oddsTraversed<randomChoice) { oddsTraversed+=oddsValues[++counter] } return counter;
 03252011, 10:36 PM #3Member
 Join Date
 Mar 2011
 Posts
 9
 Rep Power
 0
Thanks very much. Since I wanted to keep my array length flexible, I have implemented it this way. I am a newb, so if you see anything wrong or could be improved, please let me know.
//I have added 1 to arraylength to allow cumultative function below
double [] oddsValues = new double[arraylength];
int sumOfArrayLength=0;
for(int i=1; i<arraylength ;i++)
{
oddsValues[i]=i;
sumOfArrayLength+=i;
}
//converts array element i into probability (as double) by dividing array
// element number by sum of all array numbers, then makes it
//cumultative by adding previous value(oddsValues[i1])
oddsValues[0] = 0.0;
for(int i=1; i<arraylength ;i++)
{
oddsValues[i]=(oddsValues[i]/sumOfArrayLength) + oddsValues[i1];
}
Random randomnumber = new Random();
double randomChoice=randomnumber.nextDouble();
int counter=0;
while(randomChoice>oddsValues[counter])
{
counter++;
}
return counter;
Similar Threads

[Q] Generate Random Letter
By iriscience in forum New To JavaReplies: 11Last Post: 01312011, 01:10 AM 
generate pseudo random numbers in java
By csr81 in forum Advanced JavaReplies: 3Last Post: 03012010, 08:08 AM 
Generate numbers around a circle?
By pheonix in forum New To JavaReplies: 4Last Post: 06052009, 05:08 PM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 05:50 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM
Bookmarks