Random number generator always picks the same value when run inside a loop

后端 未结 5 908
慢半拍i
慢半拍i 2020-11-30 14:15

The problem with the code is, when I try to generate a number, if the spin is equal 1 it generates values inside range (1,2,3) if if try to use the loop to sum random values

5条回答
  •  醉酒成梦
    2020-11-30 15:14

    One trick is to manually create the seed by adding DateTime.Now.Ticks to the variable i:

    Random r = new Random((int)DateTime.Now.Ticks + i);
    

提交回复
热议问题