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

///
// 发现太多的随机数产生算法,都很繁。
// 其实,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);
        }
    }
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值