This article mainly introduces the C # generation of random number function, involving C # mathematical operations and string manipulation related skills, with a certain reference value, the need for friends can refer to the following
The example in this article describes the C # generation of random numbers functionality. Share to everyone for your reference, as follows:
Using system;using system.collections.generic;using system.linq;using system.text;namespace csharp{class Program {s tatic void Main (string[] args) {Console.WriteLine ("Generate random number \ n"); int randcount = 9;//number of random digits int randmin = 1;//random number minimum int randmax = 21;//random number max int randindex, flag, temp; Randindex = TEMP = flag = 0; Random rand = new Random (); int[] Randarr = new Int[randcount]; RANDARR[0] = rand. Next (Randmin, Randmax); while (true) {flag = 0; temp = rand. Next (Randmin, Randmax); for (int i = 0; I <= randindex; i++) {if (temp = = Randarr[i]) {flag = 1; Break }} if (flag = = 1)//IF flag = = 1 has duplicate number generation. {continue; } else if (flag = = 0) {randindex++; Randarr[randindex] = temp; } if (Randindex >= randCount-1)//If the Randcount exit loop is reached {break;}} for (int i = 0; i < Randcount; i++) {Console.WriteLine ("arr[" + i + "]=" + randarr[i]); } Console.WriteLine ("\ n any key exits. "); Console.ReadLine (); } }}
Generate random numbers with no duplicates
The results of the operation are as follows: