Question:

How to make a very reliable random number generator in Java?

by  |  earlier

0 LIKES UnLike

I'm doing a physics simulation that requires consistent generation of random numbers. This program must run for many hours (even days) nonstop, and it needs roughly 100000 random numbers per minute. It also needs to be used multiple times for multiple trials. Because of this, I'm afraid that simply using Math.random() isn't random enough for my purposes. Here are some of the things that I need help on:

How can I make good seeds that can allow me to run multiple simulations with very random numbers?

Because this program needs to run for a long time, will the random number generator go "stale" after a while and start returning more predictable numbers? In other words, do I have to reset my seed after some time?

Thanks in advance!

 Tags:

   Report

4 ANSWERS


  1. Hi,

    The random number returned by java will be between 0 and 1. The only way which you can get a good random number is by increasing the multiplication factor to say 100000 or more.

    0.278172 * 10 = 2 ( next random may be between 0 and 9)

    0.278172  * 100 = 27 ( next random may be between 0 and 99)

    0.278172  * 1000 = 278 ( next random may be between 0 and 999)

    0.278172  * 10000 = 2781 ( next random may be between 0 and 9999)

    0.278172  * 100000 = 27817 ( next random may be between 0 and 99999)

    the greater the multiplication factor, the lesser is the probability that the same number will be returned.

    Hope this will help.


  2. I would say that using Math.random() will be better than using a seed, it should use the same underlying functions except a seed always provides the same result(if you reseed with the same seed). Wheras if no seed is provided it will still use a seed but the seed will be the number of cpu cycles(or something like this) which changes so rapidly its very hard to get the same value twice.

    However, using the java.util.Random class to specify a seed may be better if you want to recreate the exact same simulation later  with the exact same "random" values. But if you just want as random as possible it doesnt matter, you can stick with the same seed and it would probably be fine or keep reseeding regularly if you are worried...but then to generate a seed you still need a random number generator.  Which is why the cpu clock cycles are used.

  3. The Wizard of Odds seems to have quite a good generator.

    http://wizardofodds.com/askthewizard/ran...

    There is also random.org

    http://www.random.org/

    which gives away sets of random numbers.

  4. There is class java.util.Random, may be it is rather good?

    Here is what is written in javadoc:

    "An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.) "

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.