欢迎来到我的的博客园,祝大家学有所成,早点实现自己的人生理想。

C#随机数字生成的一种方法

1、参考:

   public class RandomLongGenerater
    {
        public static long New(int bit)
        {
            if (bit > 16)
            {
                throw new Exception("bit must <= 16");
            }
            if (bit < 6)
            {
                throw new Exception("bit must >= 6");
            }
            string midStr = "";
            byte[] bytes = Guid.NewGuid().ToByteArray();
            for (int i = 0; i < bit; i++)
            {
                midStr += bytes[i].ToString().Last<char>();
            }
            if (midStr[0] == '0')
            {
                midStr = new Random().Next(1, 10).ToString() + midStr.Substring(1);
            }
            return long.Parse(midStr);
        }
        public static long New64Bit()
        {
            return New(16);
        }
        public static long New()
        {
            return New(9);
        }

    }

 

2:调用:

RandomLongGenerater.New(16);
posted @ 2016-07-29 12:25  宋兴柱  阅读(966)  评论(0编辑  收藏  举报