Get random boolean in Java

后端 未结 10 1554
情深已故
情深已故 2020-12-01 11:28

Okay, I implemented this SO question to my code: Return True or False Randomly

But, I have strange behavior: I need to run ten instances simultaneously, where every

10条回答
  •  [愿得一人]
    2020-12-01 12:05

    The easiest way to initialize a random number generator is to use the parameterless constructor, for example

    Random generator = new Random();
    

    However, in using this constructor you should recognize that algorithmic random number generators are not truly random, they are really algorithms that generate a fixed but random-looking sequence of numbers.

    You can make it appear more 'random' by giving the Random constructor the 'seed' parameter, which you can dynamically built by for example using system time in milliseconds (which will always be different)

提交回复
热议问题