Java random numbers using a seed

JavaRandomSeed

Java Problem Overview


This is my code to generate random numbers using a seed as an argument:

double randomGenerator(long seed) {
    Random generator = new Random(seed);
    double num = generator.nextDouble() * (0.5);

    return num;
}

Every time I give a seed and try to generate 100 numbers, they all are the same.
How can I fix this?

Java Solutions


Solution 1 - Java

If you're giving the same seed, that's normal. That's an important feature allowing tests.

Check this to understand pseudo random generation and seeds:

Pseudorandom number generator

> A pseudorandom number generator (PRNG), also known as a deterministic > random bit generator DRBG, is an algorithm for generating a sequence > of numbers that approximates the properties of random numbers. The > sequence is not truly random in that it is completely determined by > a relatively small set of initial values, called the PRNG's state, > which includes a truly random seed.

If you want to have different sequences (the usual case when not tuning or debugging the algorithm), you should call the zero argument constructor which uses the nanoTime to try to get a different seed every time. This Random instance should of course be kept outside of your method.

Your code should probably be like this:

private Random generator = new Random();
double randomGenerator() {
    return generator.nextDouble()*0.5;
}

Solution 2 - Java

The easy way is to use:

Random rand = new Random(System.currentTimeMillis());

This is the best way to generate Random numbers.

Solution 3 - Java

You shouldn't be creating a new Random in method scope. Make it a class member:

public class Foo {
   private Random random 

   public Foo() {
       this(System.currentTimeMillis());
   }

   public Foo(long seed) {
       this.random = new Random(seed);
   }

   public synchronized double getNext() {
        return generator.nextDouble();
   }
}

This is only an example. I don't think wrapping Random this way adds any value. Put it in a class of yours that is using it.

Solution 4 - Java

That's the principle of a Pseudo-RNG. The numbers are not really random. They are generated using a deterministic algorithm, but depending on the seed, the sequence of generated numbers vary. Since you always use the same seed, you always get the same sequence.

Solution 5 - Java

Problem is that you seed the random generator again. Every time you seed it the initial state of the random number generator gets reset and the first random number you generate will be the first random number after the initial state

Solution 6 - Java

If you'd want to generate multiple numbers using one seed you can do something like this:

public double[] GenerateNumbers(long seed, int amount) {
	double[] randomList = new double[amount];
	for (int i=0;i<amount;i++) {
		Random generator = new Random(seed);
		randomList[i] = Math.abs((double) (generator.nextLong() % 0.001) * 10000);
		seed--;
	}
	return randomList;
}

It will display the same list if you use the same seed.

Solution 7 - Java

Several of the examples here create a new Random instance, but this is unnecessary. There is also no reason to use synchronized as one solution does. Instead, take advantage of the methods on the ThreadLocalRandom class:

double randomGenerator() {
    return ThreadLocalRandom.current().nextDouble(0.5);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul BhatiaView Question on Stackoverflow
Solution 1 - JavaDenys SéguretView Answer on Stackoverflow
Solution 2 - JavaPollarView Answer on Stackoverflow
Solution 3 - JavaduffymoView Answer on Stackoverflow
Solution 4 - JavaJB NizetView Answer on Stackoverflow
Solution 5 - JavaMinion91View Answer on Stackoverflow
Solution 6 - Javauser5479540View Answer on Stackoverflow
Solution 7 - JavaFinnView Answer on Stackoverflow