I'm using the Random
class in Java as a pseudo-random number generator. I'm using the function nextDouble a lot of times (~10^5). How many times before I have to reseed to prevent from getting the same numbers? Is reseeding needed?
Random generator = new Random();
double[] numbers = new double[n];
for (int i = 0; i < n; i++) numbers[i] = generator.nextDouble();
This is for an experiment, the numbers will be used as coordinates for points on a space, so I want the distribution to be as uniform as possible.
Also how do I reseed? Where do I get the int seed from?
The random number generator will produce a random double from two random int values. The internal seed has 48 bit, so the random sequence repeats after at most 2^48 int values, or 2^47 double values.
I am sorry I can't directly answer your question. I don't remember the cycle time of Java's random number generator. Though I do think you are cutting it close with the amount of numbers you are generating.
But, what I learned in my computer engineering statistic classes might be able to help you.
I learned that the best method for generating the most random numbers is using the Mersenne Twister random number generator. This generator will provide you with enough random numbers to not need to reseed, it has a period of (2^19937) − 1
Here is source code for MerseeneTwister
https://java2s.com/Open-Source/Java/Natural-Language-Processing/MorphAdorner/edu/northwestern/at/utils/math/randomnumbers/MersenneTwister.java.htm
Here is a class to generate your random numbers.
Here is a sample to generate a random number. Please note that I removed the comments from the source. This may voliate the open source nature of the code, but I couldnt copy it all and have it formated as code.
You don't need to worry about reseeding etc if you use a Set (which guarantees unique values):
Despite what you might think, this executes quite quickly: 60 milliseconds for 100000 numbers on a my (typical) PC.
If you really want an array, you can extract it from the set.
If you want to maintain the order they were generated in, use a
LinkedHashSet
(it had similar performance)