简体   繁体   English

如何生成“随机”数和“唯一”数?

[英]How to generate “random” but also “unique” numbers?

How are random numbers generated.? 如何生成随机数? How do languages such as java etc generate random numbers, especially how it is done for GUIDs.? Java之类的语言如何生成随机数,尤其是对于GUID如何完成? i found that algorithms like Pseudorandomnumber generator uses initial values. 我发现像Pseudorandomnumber生成器这样的算法使用初始值。

But i need to create a random number program, in which a number once occurred should never repeats even if the system is restarted etc. I thought that i need to store the values anywhere so that i can check if the number repeats or not, but it will be too complex when the list goes beyond limits.? 但是我需要创建一个随机数程序,在该程序中,即使系统重新启动等,一旦出现数字就永远不会重复。我认为我需要将值存储在任何地方,以便我可以检查数字是否重复,但是当列表超出限制时,它将太复杂。

First: If the number is guaranteed to never repeat, it's not very random. 第一:如果保证数字永远不会重复,那么它不是很随机。

Second: There are lots of PRNG algorithms . 第二: PRNG算法很多。

UPDATE: 更新:

Third: There's an IETF RFC for UUIDs (what MS calls GUIDs), but you should recognize that (U|G)UIDs are not cryptographically secure, if that is a concern for you. 第三:存在用于UUIDIETF RFC (MS称为GUID),但是如果您担心,那么(U | G)UID 并不是加密安全的。

UPDATE 2: 更新2:

If you want to actually use something like this in production code (not just for your own edification) please use a pre-existing library. 如果您想在生产代码中实际使用这样的内容(不仅用于您自己的教育), 使用预先存在的库。 This is the sort of code that is almost guaranteed to have subtle bugs in it if you've never done it before (or even if you have). 如果您以前从未(或者即使有)从未做过这种代码,那么几乎可以保证其中包含细微的错误。

UPDATE 3: 更新3:

Here's the docs for .NET's GUID 这是.NET GUID文档

There are a lot of ways you could generate random numbers. 有很多方法可以生成随机数。 It's usually done with a system/library call which uses a pseudo-number generator with a seed as you've already described. 通常,这是通过系统/库调用来完成的,该调用使用了带有种子的伪数字生成器,正如您已经描述过的。

But, there are other ways of getting random numbers which involve specialized hardware to get TRUE random numbers. 但是,还有其他获取随机数的方法,其中涉及专门的硬件以获取TRUE随机数。 I know of some poker sites that use this kind of hardware. 我知道一些使用这种硬件的扑克网站 It's very interesting to read how they do it. 阅读他们的操作方式非常有趣。

Most random number generators have a way to "randomly" reïnitialize the seed value. 大多数随机数生成器都有一种方法可以“随机”地重新初始化种子值。 (Sometimes called randomize). (有时称为随机化)。

If that's not possible, you can also use the system clock to initialize the seed. 如果那不可能,您还可以使用系统时钟来初始化种子。

You could use this code sample: http://xkcd.com/221/ Or, you can use this book: http://www.amazon.com/Million-Random-Digits-Normal-Deviates/dp/0833030477 您可以使用以下代码示例: http : //xkcd.com/221/或者,您可以使用本书: http : //www.amazon.com/Million-Random-Digits-Normal-Deviates/dp/0833030477

But seriously, don't implement it yourself, use an existing library. 但是,严重的是,不要自己实现它,而要使用现有的库。 You can't be the first person to do this. 您不能成为第一个这样做的人。

Specifically regarding Java: 特别针对Java:

  • java.util.Random uses a linear congruential generator , which is not very good java.util.Random使用线性同余生成器 ,这不是很好
  • java.util.UUID#randomUUID() uses java.security.SecureRandom , an interface for a variety of cryptographically secure RNGs - the default is based on SHA-1, I believe. java.util.UUID#randomUUID()使用java.security.SecureRandom ,这是用于各种加密安全RNG的接口-我相信默认值基于SHA-1。
  • UUIDs/GUIDs are not necessarily random UUID / GUID不一定是随机的
  • It's easy to find implementations of RNGs on the net that are much better than java.util.Random , such as the Mersenne Twister or multiply-with-carry 在网上可以轻松找到比java.util.Random更好的RNG实现,例如Mersenne Twister或带乘数

I understand that you are seeking a way to generate random number using C#. 我了解您正在寻找一种使用C#生成随机数的方法。 If yes, RNGCryptoServiceProvider is what you are looking for. 如果是,则寻找RNGCryptoServiceProvider

[EDIT] [编辑]

If you generate a fairly long number of bytes using RNGCryptoServiceProvider, it is likely to be unique but there is no gurantee. 如果使用RNGCryptoServiceProvider生成相当长的字节数,则它可能是唯一的,但没有保证。 In theory, true random numbers doesnt mean to be unique. 从理论上讲,真正的随机数并不意味着唯一。 You roll a dice 2 times and you may get head both the times but they are still random. 你掷骰子2次,两次都可能得头,但它们仍然是随机的。 TRUE RANDOM! 真正的随机!

I guess to apply the check of being unique, you just have to roll out your own mechanism of keeping history of previously generated numbers. 我猜想应用唯一性检查,您只需推出自己的机制即可保留先前生成的数字的历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM