生成RNGCryptoServiceProvider随机数和随机gb2312一级中文(根据网上的修改)

5 篇文章 0 订阅

随机数用的是msdn上的例子,不过做了下修改,随机中文因为网上找到的写法很重复,所以按照gb2312标准修改了下

随机数:

/// <summary>
        /// 生成小于输入值绝对值的随机数
        /// </summary>
        /// <param name="NumSides"></param>
        /// <returns></returns>
        public static int Next(this int numSeeds)
        {
            numSeeds = Math.Abs(numSeeds);
            if (numSeeds <= 1)
            {
                return 0;
            }
            
            int length = 4;
            if (numSeeds <= byte.MaxValue)
            {
                length = 1;
            }
            else if (numSeeds <= short.MaxValue)
            {
                length = 2;
            }

            return Next(numSeeds, length);
        }

        private static int Next(int numSeeds, int length)
        {
            // Create a byte array to hold the random value.
            byte[] buffer = new byte[length];
            // Create a new instance of the RNGCryptoServiceProvider.
            System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
            // Fill the array with a random value.
            Gen.GetBytes(buffer);
            // Convert the byte to an uint value to make the modulus operation easier.
            uint randomResult = 0x0;//这里用uint作为生成的随机数
            for (int i = 0; i < length; i++)
            {
                randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
            }
            // Return the random number mod the number
            // of sides.  The possible values are zero-based
            return (int)(randomResult % numSeeds);
        }

 

随机中文:

/*
             *   gb2312 80
             * 
             *   01-09区为特殊符号。   
             *   16-55区为一级汉字,按拼音排序。  
             *   56-87区为二级汉字,按部首/笔画排序。   
             *   10-15区及88-94区则未有编码。 
         *   
         * 所有数字都是从 1 开始
             *   
             * 每个汉字及符号以两个字节来表示。
             * 第一个字节称为“高位字节”(也称“区字节)”,upper byte
             * 第二个字节称为“低位字节”(也称“位字节”)。low byte  
             * “高位字节”使用了0xA1-0xF7(把01-87区的区号加上0xA0),
             * “低位字节”使用了0xA1-0xFE(把01-94加上 0xA0)。 
             * 由于一级汉字从16区起始,汉字区的“高位字节”的范围是0xB0-0xF7,
             * “低位字节”的范围是0xA1-0xFE,
             * 占用的码位是 72*94=6768。
             * 其中有5个空位是D7FA-D7FE(55区90-94)
                */
        public static string GetRandomPopularSimplifiedChinese(this int length)
        {
            if (length <= 0)
            {
                return string.Empty;
            }

            byte minUpper = 16;
            byte maxUpper = 55;
            byte rangeLow = 94;
            int addParamer = 0xA0;

            StringBuilder tempStr = new StringBuilder();
            int tempUpper = maxUpper - minUpper + 1;
            Encoding gb = Encoding.GetEncoding("gb2312");

            for (int i = 0; i < length; i++)
            {
                int rdUpperValue = Next(tempUpper) + minUpper;
                int rdLowValue;
                do
                {
                    rdLowValue = Next(rangeLow) + 1;//索引从1开始,所以94种子生成的随机数+1
                }
                while (rdUpperValue == maxUpper && rdLowValue >= 90);//D7FA-D7FE是空位(55区90-94)

                rdUpperValue += addParamer;
                rdLowValue += addParamer;

                byte[] byteArray = new byte[] { (byte)rdUpperValue, (byte)rdLowValue };

                tempStr.Append(gb.GetString(byteArray));
            }

            return tempStr.ToString();
        }


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值