Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Random() not behaving as expected? [duplicate]

Tags:

java

random

I've been scratching my head over this for long enough, It's time for the big guns:

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

why does the above seem to print -1157793070, always?

like image 237
HellishHeat Avatar asked Feb 25 '26 15:02

HellishHeat


1 Answers

It is because you define a custom seed that is always the same with each run. From the JavaDoc:

The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method {@link #next}.

This is particularly useful for running tests.

like image 147
Nikolas Charalambidis Avatar answered Feb 27 '26 05:02

Nikolas Charalambidis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!