C# 不重复随机数的产生算法!

来源:互联网 发布:淘宝开业牌匾 编辑:程序博客网 时间:2024/05/22 02:00
///////////////////////////////////////////////////////
// 发现太多的随机数产生算法,都很繁。
// 其实,c#自带的数据算法即可很好的实现!
// 正所谓“蓦然回首,那人却在,灯火阑珊处”!
///////////////////////////////////////////////////////
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            // --------------------------------------------            // 随机数产生            // --------------------------------------------            Console.WriteLine("产生随机数1-100");            SortedList sl = new SortedList();            sl.Clear();            for (int no = 0; no < 1 * 100; no++)            {                sl[no + 1] = no + 1;            }            Console.WriteLine("整理随机数1-100");            ArrayList al = new ArrayList();            Random r = new Random();            for (int NN = 1 * 100; NN > 0; NN--)            {                if (NN == 1)                {                    al.Add(sl.GetByIndex(0));                    break;                }                int nTemp = r.Next(0, NN);                al.Add(sl.GetByIndex(nTemp));                sl.RemoveAt(nTemp);            }            string sd0;            sd0 = "";            for (int i = 0; i < al.Count; i++)            {                sd0 = sd0 + (al[i].ToString() + ";");            }            Console.WriteLine(sd0);        }    }}