如何生成唯一的字母数字? [英] How to generate Unique alphanumeric?

查看:167
本文介绍了如何生成唯一的字母数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何总能生成C#中的17个字符的唯一字母数字数据。

How can I always generate a 17 character unique alphanumeric data with c#.

推荐答案

生成新的GUID,并通过模62分才17次,每次你得到号码是上述的字符数组的索引(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890)。
使用的Guid,你能保证你的价值是独一无二的,因为GUID是。

Generate new Guid, and divide it 17 times by modulo 62. Each number you get is an index in char array described above ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890"). By using Guid you can guarantee that your value is as unique as Guid is.

如果您关心丢失的唯一位可能哈希GUID与MD5像这样的:

If you are concerned with losing unique bits you may hash GUID with MD5 like this:

Guid guidValue = Guid.NewGuid();
MD5 md5 = MD5.Create();
Guid hashed = new Guid(md5.ComputeHash(guidValue.ToByteArray()));



更新的GUID格式

根据该文献( RFC 4122 的),并与用C#生成的GUID进行比较,它们。是随机型

According to this document (RFC 4122) and comparing with GUIDs generated by C#, they are of random type.

这类型有以下模式: XXXXXXXX-XXXX-4XXX-VXXX-XXXXXXXXXXXX ,其中,

This type has the following pattern: xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx, where


  • X 是随机数和

  • V 是位布局10yy,其中yy是两个随机比特的数字。

  • x is random number and
  • V is a number with bit layout as 10yy, where yy are two random bits.

所以,在这里,我们有122个随机比特出128.所以,在独特性方面的GUID只是一个大的随机数,您可以自由使用任何其他随机数生成算法,能够产生88位的随机数(例如 RNGCryptoServiceProvider )。

So, here we have 122 random bits out of 128. So, in terms of uniqueness the Guid is simply a large random number and you are free to use any other random-number-generation algorithm that is capable to produce 88-bit random number (for example RNGCryptoServiceProvider).

当然用来生成的GUID可能在框架的未来版本中改变,但此刻的Guid.NewGuid()看起来像廉价的随机数发生器而言的方法代码。

Of course the method used to generate Guids may change in future versions of framework, but at the moment the Guid.NewGuid() looks like cheap random number generator in terms of code.

这篇关于如何生成唯一的字母数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆