5

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!

som86
  • 61
  • 4

4 Answers4

3

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
  • 5,323
  • 2
  • 24
  • 30
2

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
  • 8,162
  • 3
  • 37
  • 61
  • How is this gaussian or exponentially distributed? NOTE: I am not the downvoter. – President James K. Polk Oct 10 '10 at 13:19
  • if myRandFunction() returns numbers in gaussian distribution from 0 to 1 this will return gaussian distributed integers. – Klark Oct 10 '10 at 13:23
  • (also didn't downvote) There isn't such thing as gaussian distributed integers. Gaussian (normal) distribution is valid only in context of continuous ranges. The most similar discrete distribution is binomial. – Rekin Dec 10 '10 at 10:22
-1

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
  • 18,795
  • 6
  • 56
  • 82
  • 2
    but it will still be uniformly distributed rather than Gaussian. – Itay Karo Oct 10 '10 at 12:42
  • yes ìff the double-generator was uniformly distributed, iff it had a gaussian distribution the integer range will have a gaussian distribution too. – flownt Oct 10 '10 at 12:57
  • 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 Oct 10 '10 at 16:51
-1

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
  • 760
  • 5
  • 11
  • 1
    I didn't do it, but I think it because none of them really answers the question. – Colin Hebert Oct 10 '10 at 12:58
  • @Colin Hebert, it's not thàt hard to do it yourself approximately gaussian: rand()/4 + rand()/4 + rand()/4 + rand()/4 since the binomial distribution approximates the normal distribution, this should generate a binomial distribution between 0 and the maximum output of rand() – flownt Oct 10 '10 at 13:02