Random class acting odd?

后端 未结 6 741
粉色の甜心
粉色の甜心 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:13

    The values generated by Random class are pseudo-random: they are created using a deterministic algorithm, based on seed value. Typically (if you use parameterless constructor, for example) the seed is initialized using current time, which is obviously a unique value. Hence a unique, 'random' sequence is generated.

    Here you are using a constant seed value which doesn't change between executions of your code. Therefore you always get the same sequence. It just happens that this sequence is 1 1 1 1 1 1 ... for this particular seed.

提交回复
热议问题