How do I generate a random int number?

前端 未结 30 2666
长发绾君心
长发绾君心 2020-11-21 11:02

How do I generate a random integer in C#?

30条回答
  •  一个人的身影
    2020-11-21 11:45

    Beware that new Random() is seeded on current timestamp.

    If you want to generate just one number you can use:

    new Random().Next( int.MinValue, int.MaxValue )

    For more information, look at the Random class, though please note:

    However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers

    So do not use this code to generate a series of random number.

提交回复
热议问题