Random class acting odd?

后端 未结 6 758
粉色の甜心
粉色の甜心 2020-12-03 21:33

In this code:

Random random = new Random(441287210);
for(int i=0;i<10;i++)
    System.out.print(random.nextInt(10)+\" \");
}

The output

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 22:12

    Random class uses seed to generate random number when you call nextInt() and is advised to be a long number, when you are creating random object, you are providing an int which is not sufficient enough for randomness.

    Try to run the loop for 20 times, you will see randomness or remove seed or provide a very long seed value

提交回复
热议问题