[Solved] What is a Random object seed [duplicate]


It’s not just a java thing.

It’s very hard to let a computer generate a real random number. Your computer needs to perform a complex unpredicatble calculation. Your seed value will act as an input for these calculations.

A lot of systems will use a timestamp as a seed. Because that’s a value that will be different every time you run it. But let’s say you do specify a seed, and that you use the same seed multiple times:

    Random rnd = new Random(10);
    System.out.println(rnd.nextInt());
    System.out.println(rnd.nextInt());
    System.out.println(rnd.nextInt());

    // do it again with the same seed
    rnd = new Random(10);
    System.out.println(rnd.nextInt());
    System.out.println(rnd.nextInt());
    System.out.println(rnd.nextInt());

This code will print the same 3 values 2 times.

Output:

-1157793070
1913984760
1107254586
-1157793070
1913984760
1107254586

So, if you reuse a seed value, it will generate the same numbers.

solved What is a Random object seed [duplicate]