一、Random类
在C#中,System.Random类用于产生随机数。
Random类的Next()方法用于返回非负随机数;
Next(int)方法用于返回一个小于所指定最大值的非负随机数;
Next(int,int)方法用于返回一个指定范围内的随机数;(随机数可取下界值,但不可取上界值)
NextDouble()方法用于返回一个介于0.0和1.0之间的随机数
二、随机产生10个数,每个数在1~50范围内,要求每个数不同。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Test 8 { 9 class T 10 { 11 static void Main(string[] args) 12 { 13 int[] a = new int[10]; 14 Random ran = new Random(); 15 for (int i = 0; i < a.Length; i++) 16 { 17 one_num://标签,配合goto语句使用 18 a[i] = (int)ran.Next(50) + 1; 19 for (int j = 0; j < i; j++) 20 { 21 if (a[i] == a[j]) 22 goto one_num;//one_num 23 } 24 } 25 foreach (int n in a) 26 Console.Write("{0}\0", n); 27 while (true); 28 } 29 } 30 }