C generates random number instances
- 2020-12-16 06:04:55
- OfStack
This article gives an example of how C# generates random numbers. Share to everybody for everybody reference. The details are as follows:
/// <summary>
/// generate num Digit verification code Used for short message verification function
/// </summary>
public static Random rand = new Random();
public static string getCode(int num)
{
string result = "";
string s = "uabcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < num; i++)
{
int k = ((rand.Next(1, 36) + i * 6) % 36) + 1;
result = string.Format("{0}{1}", result, s[k]);
} return result;
}
Random r = new Random(); // create 1 Random number generator
int v= r.Next(1, 101); // get 1 a Less than or equal to 1 And less than 101 Random number. That is: 1-100 Between the values of (including 1 , and 100 )
Hopefully this article has helped you with your C# programming.