本文转自:
class Program
{ static void Main(string[] args) { int[] Arr = new int[100]; Random random = new Random(); for (int i = 0; i < Arr.Length; i++) { Arr[i] = GetRandomNum(Arr, random); } for (int i = 0; i < 1000; i++) { //Console.WriteLine(random.Next(101));//这种会产生0-100之间的数,返回一个小于指定数的非负随机数 //Console.WriteLine(random.Next(1,101));//这种会产生1-100之间的数 } sortArr(Arr, false); foreach (var item in Arr) { Console.WriteLine(item); }}
private static int GetRandomNum(int[] Arr, Random random)
{ int tempValue = random.Next(1, 101); if (Arr.Contains(tempValue)) { tempValue = GetRandomNum(Arr, random); } return tempValue; }private static int GetRandomNum2(int[] Arr, Random random)
{ int tempValue = random.Next(1, 100); while (!Arr.Contains(tempValue)) { tempValue = random.Next(1, 100); } return tempValue; }
static void sortArr(int[] Arr, bool orderby)
{ for (int i = 0; i < Arr.Length; i++) { for (int j = i + 1; j < Arr.Length; j++) { if (orderby) { if (Arr[i] > Arr[j]) { Arr[i] = Arr[j] + (Arr[j] = Arr[i]) * 0; } } else { if (Arr[i] < Arr[j]) { Arr[i] = Arr[j] + (Arr[j] = Arr[i]) * 0; } } } } } }