Abhishek Singh
Abhishek Singh

Reputation: 10229

Java unusual behaviour of random numbers

I came across a code snippet on internet. Here it goes

  public class Test {

    public static void main(String[] args) {
    Random random = new Random(441287210);  

    for(int j=0;j<10;j++) { 

           System.out.print(random.nextInt(10)+" ");  

    } 
}

}

Everytime i run it it prints 1 1 1 1 1 1 1 1 1 1 . There might be a strong reason for it.

Why is this behaviour observed.

Here is the source --> http://www.javacodegeeks.com/2011/10/weird-funny-java.html

Upvotes: 0

Views: 144

Answers (2)

BobTheBuilder
BobTheBuilder

Reputation: 19284

Every Random seed generates identical sequences of numbers. 441287210 seed generates this sequence, just as any other generated sequence...

From documentation:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers

Upvotes: 1

You're initializing the pseudorandom number generator to a specific state, which means that it will always produce the same output across runs. It looks like someone just found a seed that happens to produce an interesting series of results.

Upvotes: 8

Related Questions