This article introduces a C# function written by me yourself, which is used to generate a random number between two numbers.
There is a Random class in C#, which can easily generate random numbers. In fact, one of the most important ways to generate is to give it a maximum value and a minimum value, and the system can automatically generate a random number in the range.
When creating a Random object, you need to assign a random number seed. This article will not introduce what the meaning of random number seeds. Anyway, if there are no seeds, the random number obtained each time will be the same random number (it's very nonsense). Here we use the NewGuid() method of the Guid class to create a random character, and this string will not be the same. This Guid random string is so perfect to get the seed of random numbers!
The function code for generating random numbers is as follows:
public int GetRandNum(int min, int max)
{
Random r = new Random(().GetHashCode());
return (min, max);
}
Enter a minimum and a maximum value and you will get a random number between the minimum and the maximum value.
This article introduces so much about generating random numbers between two numbers in C#. I hope it will be helpful to you. Thank you!