使用一个GUID一个有效的方法来生成一个随机字符串的字符和数字? [英] Is using a GUID a valid way to generate a random string of characters and numbers?

查看:406
本文介绍了使用一个GUID一个有效的方法来生成一个随机字符串的字符和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何随机是System.Guid.NewGuid()?

Possible Duplicate:
How Random is System.Guid.NewGuid()?

href="http://stackoverflow.com/questions/292254/vb6-how-do-i-make-a-random-string-of-0-9-and-a-z-of-x-characters/">this问题我想如果使用GUID来生成一个随机字符串的字符和数字就知道有什么破绽呢?

Based on this question I would like to know if using a GUID to generate a random string of characters and numbers has any flaws in it?

因此​​,举例来说,如果我想要一个随机字符串的32个字符,我可以用下面的C#code字符和数字的:

So, for example, if I wanted a random string of characters and numbers of 32 or fewer characters I could use the following C# code:

string s = Guid.NewGuid().ToString().Replace("-", "");

如果长度需要比32短我会截断字符串,如果它需要更长我会添加多个GUID的在一起。

If the length needed to be shorter than 32 I would truncate the string and if it needed to be longer I would add multiple GUID's together.

什么是这种方法的缺陷?

What are the flaws in this approach?

在我写这我意识到,一个缺陷是,它会永远只能有字母A到F,所以我将修改的问题:

After I wrote this I realized that one flaw would be that it would only ever have the letters a through f so I will modify the question:

这是6个字符和10位真正的随机序列?

Is this a truly random sequence of 6 characters and 10 digits?

推荐答案

一个GUID不做出随机性的保证,这让周围的保证唯一性。如果你想随机性,可使用随机生成的字符串。

A GUID doesn't make guarantees about randomness, it makes guarantees around uniqueness. If you want randomness, use Random to generate a string.

这篇关于使用一个GUID一个有效的方法来生成一个随机字符串的字符和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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