Random number generator that generates integers for Java
I want to generate开发者_如何学JAVA some random integers in Java, but this according to some distribution laws. More specific:
I want to generate some random integers for gaussian distribution. I found out only generators which return double results for the gaussian distribution. Why is that?
I want to generate some random integers between some limits for exponential distribution? Here I also found out only about generators which return double. I also didn't find out a way to generate some random exponential numbers only between two limits.
Can you help me? Do you know a library which can do what I want? I studied Michael Flanagan's library, colt and apache's Commons Math but they don't have what I need.
Thanks!
I suggest you to use the Uncommons Maths library, which comprises different random generators (e.g. Mersenne Twister, AES-based) and distributions (poisson, gaussian and so on)
As for the "double problem": almost all random generators generate double because they are the most used. If you need integers you'll need to do the rounding yourself (a call to Math.round will be enough). Let's say that you are generating random people heights with centimeter accuracy: if your random generator returns 175.234, you can just round it to 175. That's really not a problem.
As for the limits for exponential distribution: there are no generators that let you choose limits because no such limits exist for exponential distribution. An exponential distribution typically models the delays between two consecutive events in a Poisson process: the delay can be as low as 0, or can be extremely high. The extremely high outcomes are really really unlikely, but they are not impossible. you can solve the problem by getting a random number from the generator, adding your lower limit and using Math.max to trim it if it is higher than your upper limit. But this is no longer an exponential distribution.
If you have a double number from 0 to 1 you can scale it to the integer:
int res = lowLimit + (int)(myRandFunction() * (highLimit - lowLimit));
Edit:
Why I got a vote down? He sad he has a function that returns a double in distribution he wants (I guessed a double form 0 to 1), so this is going to do the job.
Just generate a double and scale it to the integer range you require. For example, if a regular (uniform) random number generator generates numbers from 0.0 to 1.0 and you want numbers from 0 to 100, you'd just multiply the generated random number by 100.
boost has some really nice random number generators, i know its c++ and not java, but the implementations should be a good guide to implementing them yourself.
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_random.html
精彩评论