How do I generate a random int number?

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

How do I generate a random integer in C#?

30条回答
  •  无人及你
    2020-11-21 11:47

    Try these simple steps to create random numbers:

    Create function:

    private int randomnumber(int min, int max)
    {
        Random rnum = new Random();
        return rnum.Next(min, max);
    }
    

    Use the above function in a location where you want to use random numbers. Suppose you want to use it in a text box.

    textBox1.Text = randomnumber(0, 999).ToString();
    

    0 is min and 999 is max. You can change the values to whatever you want.

提交回复
热议问题