Generates four random numbers for login verification code creation ., Four-digit random number
You can call either of the following two methods:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace SJ_random 8 {9 class Class_main10 {11 static void Main () 12 {13 Class_random rdm = new Class_random (); 14 15 Random rm = new Random (); 16 17 string s = string. empty; // Empty string. 18 19 Console. writeLine (rdm. random_1 (s, rm); // call method random_1 to generate a random number. 20 21 Console. write (Rdm. random_2 (s, rm); // call method random_2 to generate a random number. 22 23 Console. readKey (); 24} 25} 26 27 public class Class_random28 {29 /// <summary> 30 /// Method 1: generate a random number. 31 /// </summary> 32 // <param name = "s"> receives the generated random number </param> 33 /// <param name = "rm"> random instance </param> 34 // random number generated by <returns> </returns> 35 public string random_1 (string s, random rm) 36 {37 for (int I = 0; I <4; I ++) 38 {39 int k = rm. next (); 40 41 char j = (char) ('0' + (char) (k % 10); 42 43 s + = j. toString (); 44} 45 return s; 46} 47 48 // <summary> 49 // Method 2: generate a random number. 50 /// </summary> 51 // <param name = "s"> receives the generated random number </param> 52 /// <param name = "rm"> random instance </param> 53 // random number generated by <returns> </returns> 54 public string random_2 (string s, random rm) 55 {56 s = Convert. toString (rm. next (1000,999 9); 57 58 return s; 59} 60} 61}