北京网站建设-恒动时空
将网上查到的资料收集整理如下:
1、利用GUID生成全球唯一标识符,长度为32位,格式如:6F9619FF-8B86-D011-B42D-00C04FC964FF。
SQLSERVER本身可以生成,ASP.NET也可以生成。
最简单的如:GUID guid = Guid.newguid()
2、将GUID转为哈希数,然后随机
Random rand = new Random(Guid.NewGuid().GetHashCode());
生成示例:1024588704
3、获取系统启动后经过的毫秒数,然后随机
Random rnd = new Random(Environment.TickCount);
生成示例:208439635
4、其他利用GUID生成的的方法示例:
private long GenerateId() { byte[] buffer = Guid.NewGuid().ToByteArray(); return BitConverter.ToInt32(buffer, 0); }
生成:5020365391994185848
private string GenerateId(string flag) { long i = 1; foreach (byte b in Guid.NewGuid().ToByteArray()) { i *= ((int)b + 1); } return string.Format("{0:x}", i - DateTime.Now.Ticks); }
生成:aded0a2611f8aa4a
5、两个用于生成不重复数字的类
public class RandomNum { private static char[] constant ={ '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; public static string GenerateRandom(int Length) { System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62); Random rd = new Random(); for (int i = 0; i < Length; i++) { newRandom.Append(constant[rd.Next(62)]); } return newRandom.ToString(); } }
生成:EV34SOG1RGCV(可控制生成位数)
/// <summary> /// 生成不重复乱码 /// </summary> public class RandomNumber { const long MAX = 218340105584895; // ZZZZZZZZ const long MIN = 568002355840; // 0a0000000 const int SeedQuantity = 1; // 随机质量,越大越慢,即使为1质量也不错 public static long[] getRandom(int size) { //先取一定倍量的随机种子,再从这些种子里面随机 long[] seed = getRandomSeeds(size * SeedQuantity); long[] ranArr = new long[size]; Random rand = new Random(); for (int i = 0; i < size; i++) { int j = rand.Next(seed.Length - i); ranArr[i] = seed[j]; seed[j] = seed[seed.Length - 1 - i]; } return ranArr; } private static long[] getRandomSeeds(int size) { long[] ranArr = new long[size]; for (int i = 0; i < size; i++) { ranArr[i] = GetNext(i); } Array.Sort(ranArr); // 排序 List<int> seedRepeatPosition = new List<int>(); // 遍历一遍将重复的记录下来 long last = 0; for (int i = 0; i < ranArr.Length; i++) { if (ranArr[i] == last) { ranArr[i] = 0; seedRepeatPosition.Add(i); } else last = ranArr[i]; } Array.Sort(ranArr); // 排序 // 将重复的重新处理为不重复的 List<long> newseedList = new List<long>(); long newseed; for (int i = 0; i < seedRepeatPosition.Count; i++) { do { newseed = GetNext(i); } while (Array.BinarySearch(ranArr, newseed) > -1 || newseedList.Contains(newseed)); newseedList.Add(newseed); ranArr[seedRepeatPosition[i]] = newseed; } return ranArr; } private static long GetNext(int seed) { int initSeed = (int)(DateTime.Now.Ticks - seed * 10000); Random ran = new Random(initSeed); double d; long ret; do { d = ran.NextDouble(); ret = Convert.ToInt64(d * (Math.Pow(10, 15))); } while (ret <= MIN || ret > MAX); return ret; } }
生成示例:217654655788864
6、其它
还可以利用当前时间的Ticks属性来获取不重复的数字,
如:DateTime.Now.Ticks
生成示例:634903854555468750
或者利用MD5
7.取时间戳生成随即数生成10位流水号
/// <summary> /// 取时间戳生成随即数生成10位流水号 /// </summary> /// <returns></returns> public static UInt32 UnixStamp() { TimeSpan ts = DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); return Convert.ToUInt32(ts.TotalSeconds); }
8.随机数生成方法
/// <summary> /// 取随机数 /// </summary> /// <param name="length"></param> /// <returns></returns> public static string BuildRandomStr(int length) { Random rand = new Random(); int num = rand.Next(); string str = num.ToString(); if (str.Length > length) { str = str.Substring(0, length); } else if (str.Length < length) { int n = length - str.Length; while (n > 0) { str.Insert(0, "0"); n--; } } return str; }
9.C# 生成一个时间戳
/// 获取时间戳 /// </summary> /// <returns></returns> public static string GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString();
}