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);
宋兴柱:转载内容,请标明出处,谢谢!源文来自 宝贝云知识分享:https://www.dearcloud.cn