Results 1 to 3 of 3
- 03-25-2011, 12:42 PM #1
Member
- 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.
- 03-25-2011, 04:49 PM #2
Senior 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;
- 03-25-2011, 09:36 PM #3
Member
- 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[i-1])
oddsValues[0] = 0.0;
for(int i=1; i<arraylength ;i++)
{
oddsValues[i]=(oddsValues[i]/sumOfArrayLength) + oddsValues[i-1];
}
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: 01-31-2011, 12:10 AM -
generate pseudo random numbers in java
By csr81 in forum Advanced JavaReplies: 3Last Post: 03-01-2010, 07:08 AM -
Generate numbers around a circle?
By pheonix in forum New To JavaReplies: 4Last Post: 06-05-2009, 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: 04-19-2009, 05:50 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks