views:

155

answers:

4

hi,

I want to generate 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!

A: 

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.

dty
but it will still be uniformly distributed rather than Gaussian.
Itay
yes ìff the double-generator was uniformly distributed, iff it had a gaussian distribution the integer range will have a gaussian distribution too.
flownt
Why the downvote? The OP said they had a Gaussian random number generator. I was pointing out that the number can be translated from one Gaussian distribution to another. I can't remember the maths, but it's a simple transformation.
dty
+1  A: 

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.

Klark
How is this gaussian or exponentially distributed? NOTE: I am not the downvoter.
GregS
if myRandFunction() returns numbers in gaussian distribution from 0 to 1 this will return gaussian distributed integers.
Klark
A: 

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

flownt
why are all the answers downvoted??
flownt
@flownt, I didn't do it, but I think it because none of them really answers the question.
Colin Hebert
@Colin Hebert, it's not thàt hard to do it yourself approximately gaussian:rand()/4 + rand()/4 + rand()/4 + rand()/4since the binomial distribution approximates the normal distribution, this should generate a binomial distribution between 0 and the maximum output of rand()
flownt
+2  A: 

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.

Giuseppe Cardone