随机数生成器
Posted 我没有领悟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机数生成器相关的知识,希望对你有一定的参考价值。
public static class RandomHelper { /// <summary> /// 随机数生成器 /// </summary> public static Random Generator { get; } = new Random(SystemRandomInt()); /// <summary> /// 使用RNGCryptoServiceProvider生成真正随机的二进制数据 /// </summary> public static byte[] SystemRandomBytes() { byte[] bytes = System.Guid.NewGuid().ToByteArray(); return bytes; } /// <summary> /// 使用RNGCryptoServiceProvider生成真正随机的整数 /// </summary> public static int SystemRandomInt() { return BitConverter.ToInt32(SystemRandomBytes(), 0); } /// <summary> /// 生成10位数的编号 /// </summary> public static string GenerateSerial() { //把guid转换为两个long int byte[] bytes = System.Guid.NewGuid().ToByteArray(); UInt64 high = System.BitConverter.ToUInt64(bytes, 0); UInt64 low = System.BitConverter.ToUInt64(bytes, 8); //转换为10位数字 UInt64 mixed = ((high ^ low) % 8999999999) + 1000000000; return mixed.ToString(); } public static string GenerateOrderId() { long id = DateTime.Now.Ticks - DateTime.Parse("2017/01/01").Ticks; return id.ToString("D10"); } /// <summary> /// 生成随机字符串 /// </summary> /// <param name="length">字符串长度</param> /// <param name="chars">包含的字符,默认是a-zA-Z0-9</param> /// <returns></returns> public static string RandomString(int length, string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") { var buffer = new char[length]; for (int n = 0; n < length; ++n) { buffer[n] = chars[Generator.Next(chars.Length)]; } return new string(buffer); } }
以上是关于随机数生成器的主要内容,如果未能解决你的问题,请参考以下文章