起先对于生成6位不重复随机数,包含并发环境,后期做了一些方法整理,各个环境会使用到;
第一种:GUID生成:
/// <summary>
/// 生成唯一ID
/// </summary>
/// <returns></returns>
internal static string BuildUniqueId(int digits)
{
return
new Random(Guid.NewGuid().GetHashCode() + int.Parse(DateTime.Now.ToString("fffff"))).Next((int)Math.Pow(10, digits)).ToString(CultureInfo.InvariantCulture).PadRight(digits, '0');
}
第二种:加密生成
/// <summary>
///加密生成
/// </summary>
/// <returns></returns>
private static int NewMethod()
{
var strs = Guid.NewGuid().GetHashCode().ToString() + DateTime.Now.ToString("ffffff");
byte[] buffer = Encoding.UTF8.GetBytes(strs);
// 创建一个新的随机数生成器。
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
// 用随机值填充数组。
Gen.GetBytes(buffer);
Int32 numstr = Math.Abs(BitConverter.ToInt32(buffer, 0) % (int)Math.Pow(10, 10));
return numstr;
}
包含(2种如果有兴趣的同学可以共同学习,提供更好方法):
private static int Next(int numSeeds, int length)
{
// 创建一个字节数组来存储的随机值。
byte[] buffer = new byte[length];
// 创建一个新的随机数生成器。
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
// 用随机值填充数组。
Gen.GetBytes(buffer);
//Int32 rngNum = Math.Abs(BitConverter.ToInt32(buffer, 0) % numSeeds);
//return rngNum;
// 将字节一个UINT值进行取模运算
//这里用uint作为生成的随机数
uint randomResult = 0x0;
for (int i = 0; i < length; i++)
{
randomResult |= ((uint)buffer[i] << ((length - 1 - i)));
}
//返回随机数
return (int)(randomResult % numSeeds);
}
/// <summary>
/// 生成指定位数的字符串
/// </summary>
/// <param name="numSeeds">种子值</param>
/// <param name="maxsize">指定几位数</param>
/// <returns></returns>
public static string Getuniquekey(string numSeeds, int maxsize)
{
char[] chars = new char[62];
chars = numSeeds.ToCharArray();
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
data = new byte[maxsize];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(maxsize);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]) ;
}
return result.ToString();
}
第三种:random
private static List<int> NewMethod1()
{
List<int> listInt = new List<int>();
for (int i = 0; i < 100000; i++)
{
int rdom = 0;
Random random = new Random(Guid.NewGuid().GetHashCode() + unchecked((int)DateTime.Now.Ticks + int.Parse(DateTime.Now.ToString("fffff"))));
rdom = random.Next((int)Math.Pow(10, 6));
while (listInt.Contains(rdom))
{
rdom = random.Next((int)Math.Pow(10, 6));
}
listInt.Add(rdom);
//rdom = Int32.Parse(rdom.ToString().PadLeft(6, '1').ToString());
}
//= "JR" + DateTime.Now.ToString("yyyyMMddHHmmssff") + listInt.ToString().PadLeft(10, '0');
return listInt;
}
Dictionary<int, int> 这个速度快:
#region Dictionary
Stopwatch watch = new Stopwatch();
watch.Start();
int[] list = GetRandomIntValues(1000000);
for (int i = 0; i < list.Length; i++)
{
Console.WriteLine(i);
}
listInt = list.ToList();
watch.Stop();
Console.WriteLine(watch.Elapsed.TotalSeconds);
#endregion
private static int[] GetRandomIntValues(int expectedCouont)
{
Dictionary<int, int> container = new Dictionary<int, int>(expectedCouont);
Random r = new Random(Guid.NewGuid().GetHashCode() + int.Parse(DateTime.Now.ToString("fffff")));
while (container.Count < expectedCouont)
{
int value = r.Next();
if (!container.ContainsKey(value))
{
container.Add(value, value);
}
}
int[] result = new int[expectedCouont];
container.Values.CopyTo(result, 0);
return result;
}
第四种 RandomStringUtils 工具类
#region RandomStringUtils 类生成随机数
//var STR = DateTime.Now.ToString("yyyyMMddHHmmssfff").ToCharArray();
//Console.WriteLine(STR);
//RandomStringUtils.RandomStringUtils.Random(6, STR);
//Console.WriteLine(RandomStringUtils.RandomStringUtils.Random(6));
#endregion
第五种:Enumerable.Range
#region Enumerable.Range
//for (int i = 0; i < Enumerable.Range(100000, 1000000).OrderBy(x => Guid.NewGuid()).Count(); i++)
//{
// Console.WriteLine(i);
//}
#endregion
以上作为参考,如果有好的方法请大家分享:其中目前使用第二种:
最终采取一种,10位数 100000重复率0.005%左右;
6位为4.807%前后(趋于稳定);
7位数字为0.5%左右;
8位数字为0.05左右;
9位数字为为0.005左右