Java random numbers not random?

后端 未结 4 509
感情败类
感情败类 2021-01-12 02:43

I was trying to explain the random number generator in Java to a friend when he kept getting the same numbers every time he ran the program. I created my own simpler version

4条回答
  •  情深已故
    2021-01-12 03:20

    This:

       Random rand = new Random(100);
    

    You're giving the random number generator the same seed (100) each time you start the program. Give it something like the output from System.currentTimeMillis() and that should give you different numbers for each invocation.

提交回复
热议问题