C# -- 随机数(Random)的使用
使用随机数产生一组大乐透号码
1. C#代码
1 Console.WriteLine("===============大乐透===红色球==============="); 2 List<int> listRed = new List<int>(); 3 Random rangdomRed = new Random(); 4 for (int i = 0; listRed.Count < 5; i++) 5 { 6 int redNum = rangdomRed.Next(1, 35); 7 if (!listRed.Contains(redNum)) 8 { 9 listRed.Add(redNum); 10 Console.Write("{0} ", redNum); 11 } 12 13 } 14 Console.WriteLine(); 15 Console.WriteLine("===============大乐透===蓝色球================"); 16 List<int> listBlue = new List<int>(); 17 Random rangdomBlue = new Random(); 18 for (int i = 0; listBlue.Count < 2; i++) 19 { 20 int blueNum = rangdomBlue.Next(1, 12); 21 if (!listBlue.Contains(blueNum)) 22 { 23 listBlue.Add(blueNum); 24 Console.Write("{0} ", blueNum); 25 } 26 } 27 28 Console.WriteLine(); 29 Console.WriteLine("===============排序============="); 30 listRed.Sort(); 31 for (int i = 0; i < listRed.Count; i++) 32 { 33 Console.Write("{0} ", listRed[i]); 34 } 35 listBlue.Sort(); 36 for (int i = 0; i < listBlue.Count; i++) 37 { 38 Console.Write("{0} ", listBlue[i]); 39 } 40 Console.WriteLine(); 41 42 43 Console.ReadKey();
2. 运行结果