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);