How do I generate a random int number?

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

How do I generate a random integer in C#?

30条回答
  •  猫巷女王i
    2020-11-21 11:43

    create a Random object

    Random rand = new Random();
    

    and use it

    int randomNumber = rand.Next(min, max);
    

    you don't have to initialize new Random() every time you need a random number, initiate one Random then use it as many times as you need inside a loop or whatever

提交回复
热议问题