How do I generate a random int number?

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

How do I generate a random integer in C#?

30条回答
  •  遇见更好的自我
    2020-11-21 11:50

    While this is okay:

    Random random = new Random();
    int randomNumber = random.Next()
    

    You'd want to control the limit (min and max mumbers) most of the time. So you need to specify where the random number starts and ends.

    The Next() method accepts two parameters, min and max.

    So if i want my random number to be between say 5 and 15, I'd just do

    int randomNumber = random.Next(5, 16)
    

提交回复
热议问题