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.