天天看點

實作随機數不重複

/// <summary>
            /// 實作30個數的随機數不重複
            /// </summary>
            int[] intArr = new int[30];

            ArrayList myList = new ArrayList();

            Random rnd = new Random();

            while (myList.Count < 30)
            {

                int num = rnd.Next(1, 101);

                if (!myList.Contains(num))

                    myList.Add(num);
            }

            myList.Reverse();
            myList.Sort();
            for (int i = 0; i < 30; i++)

                intArr[i] = (int)myList[i];

            foreach (int j in intArr)
            {
                Console.Write(j + "\t");
            }
                Console.ReadKey();