CHowdappaM - 13 days ago 9

Java Question

If I set seed in Random why always get same random number in below code:

`private static void createArray(int[] x) {`

for(int i =0; i<x.length; i++){

Random random = new Random(500l);

x[i] = random.nextInt(100000); //53695

}

}

I am getting 53695 for every run and entire loop.

Answer

Because that's what happens when you use the same seed in a pseudo-randomnumber generator. It's not random, it just looks "random enough", but it's all thanks to a deterministic mathematical formula.

Use `SecureRandom`

if you need better randomness.

Here are some examples of seeds that provide "interesting" "random" numbers: http://insights.dice.com/2014/01/24/generating-random-numbers-javas-random-class/