一个C#随机数的问题,解决随机数重复

默认情况下,.NET的随机数是根据系统时间来生成的,如果电脑速度很快的话,生成的随机数就会一样。
Random rnd = new Random();
int rndNum = rnd.Next();         //int 取值范围内的随机数
int rndNum = rnd.Next(10);       //得0~9的随机数
int rndNum = rnd.Next(10,20);    //得10~19的随机数
int rndNum = rnd.NextDouble();   //得0~1的随机数  
若随机种子为系统时间,用循环一次生成多个随机数.
因为CPU运算速度太快了,所以每次取到的都是同一个时间.即生成的数字都一样了.
所以要不停地变换种子.
        public string GetRandomCode()
         {
             char[] chars = {
                                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S',
                                'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'
                            };

             string code = string.Empty;

             for (int i = 0; i < 4; i++)
             {

                 //这里是关键,传入一个seed参数即可保证生成的随机数不同            
                 //Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
                 Random rnd = new Random(GetRandomSeed( ));
                 code += chars[rnd.Next(0, 30)].ToString();
             }

             return code;
         }

        /// <summary>
        /// 加密随机数生成器 生成随机种子
        /// </summary>
        /// <returns></returns>

    staticint GetRandomSeed()

    {

        byte[] bytes = newbyte[4];

        System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();

        rng.GetBytes(bytes);

        returnBitConverter.ToInt32(bytes, 0);

    }

获取指定数量的随机组合 洗牌程序 思路

    public static IList<string> CreateChargeCodeNo(string PromotionChargeCodeNo, int Count)

    {

        List<string> lis = new List<string>();

        if (string.IsNullOrEmpty(PromotionChargeCodeNo))

        {

            return lis;

        }

        string ChargeCodeNo = PromotionChargeCodeNo;

        int length = 10 - PromotionChargeCodeNo.Length;

        while (lis.Count < Count)

        {

            int[] numbers = new int[length * 2];

            for (int i = 0; i < length * 2; i++)

                numbers[i] = i + 1;

            for (int i = 0; i < length * 2; i++)//二倍体洗牌

            {

                Random rand = new Random(GetRandomSeed());

                int temp = rand.Next(length * 2);

                int tempNumber = numbers[i];

                numbers[i] = numbers[temp];

                numbers[temp] = tempNumber;

            }

            string code = "";

            for (int x = 0; code.Length < length; x++)

            {

                code += numbers[x];

            }

            code = code.Substring(0, length);

            string s = ChargeCodeNo + code;

            if (lis.Contains(s))

            {

                continue;

            }

            lis.Add(ChargeCodeNo + code);

        }

        return lis;

    }


 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值