Generation of the same sequence of random numbers

后端 未结 5 1860
广开言路
广开言路 2021-01-07 20:42

I want to generate random numbers in java, I know I should use existing methods like Math.random(), however, my question is: how can I generate the same sequence of numbers,

5条回答
  •  一生所求
    2021-01-07 21:24

    You need to seed the random number generator.

    Random random = new Random(aFixedNumber);
    random.nextInt();  // will always be the same
    

提交回复
热议问题