简体   繁体   English

mt_rand()是否两次生成相同的数字?

[英]Does mt_rand() generate same number twice?

I am trying to generate unique token id, can i use mt_rand() ? 我正在尝试生成唯一的令牌ID,我可以使用mt_rand()吗?

will mt_rand() generate same number twice? mt_rand()生成两次相同的数字吗?

This is the first time ever I'm going to answer a question with just a comic , just because it's the right answer: 这是我第一次用漫画来回答问题,只是因为这是正确的答案:

在此处输入图片说明

The original on Dilbert.com . 原件在Dilbert.com上

mt_rand will generate the same number twice, yes. mt_rand将两次生成相同的数字,是的。 Every random number generator will probably do so eventually. 每个随机数生成器最终都可能会这样做。 (Theoretically) every number has the same chance of being generated every time the generator is run. (理论上)每次生成器运行时,每个数字都有相同的生成机会。 It could randomly generate the same number many times in a row. 可以连续多次随机生成相同的数字。 It's random. 这是随机的。

To use a random number generator for unique ids, the probability of generating the same number twice must be so low as to be irrelevant in practice. 要将随机数生成器用于唯一ID,两次生成相同数字的概率必须非常低,以至于在实践中不相关。 In this regard, mt_rand is probably not sufficient. 在这方面, mt_rand可能还不够。 The concept of randomly generated unique ids has been formalised into UUIDs , which you should use for exactly the purpose of a universally unique id . 随机生成的唯一ID的概念已正式化为UUID ,您应该将其用于通用唯一ID的确切目的。

Take this quote : 这个报价

...only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. ...仅在接下来的100年中每秒生成10亿个UUID之后,仅创建一个副本的可能性约为50%。

Since mt_rand returns a 32-bit integer on 32-bit systems, it can only return 2^32 unique values, which is a mere 4,294,967,296 unique values. 由于mt_rand在32位系统上返回32位整数,因此它只能返回2 ^ 32个唯一值,这仅是4,294,967,296个唯一值。 If you were generating a billion mt_rand values every second, you're basically guaranteed a duplicate after 4 seconds. 如果您每秒生成十亿个mt_rand值,则基本上可以保证在4秒后重复一次。 That hopefully illustrates the difference of scale between UUIDs and mt_rand and why that matters. 这有望说明UUID和mt_rand之间的规模差异以及为什么如此重要。 Even if you're generating much fewer than 1 billion ids every second, you'll still need to choose an algorithm which makes collisions practically impossible, not just unlikely. 即使每秒生成的ID少于10亿个ID,您仍然需要选择一种算法,该算法实际上使碰撞成为不可能,而不仅仅是不可能。

mt_rand() will return a random number on each call. mt_rand()将在每次调用时返回一个随机数。

But eventually, it will return numbers that have already been returned to you. 但最终,它将返回已经返回给您的号码。 That is because, along with being consistent with desired statistical properties of randomness, the generator has a finite (although for Mersenne Twister, a very large) periodicity . 这是因为,除了与所需的随机性统计特性保持一致之外,生成器还具有有限的(尽管对于Mersenne Twister而言,这是非常大的) 周期性 If this behaviour is undesirable then your best bet is to shuffle a unique set using that generator. 如果这种行为是不受欢迎的,那么最好的选择就是使用该生成器来随机播放一个唯一的集合。

mt_rand() has the following caveats: mt_rand()具有以下警告:

Caution : This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. 警告 :此函数不会生成加密的安全值,并且不应将其用于加密目的。 If you need a cryptographically secure value, consider using openssl_random_pseudo_bytes() instead. 如果需要加密安全的值,请考虑改用openssl_random_pseudo_bytes()。

And

Caution : The distribution of mt_rand() return values is biased towards even numbers on 64-bit builds of PHP when max is beyond 2^32. 注意 :当max超过2 ^ 32时,mt_rand()返回值的分布在PHP的64位版本上偏向偶数。

If you're fine with it not being completely unbiased, you should be ok. 如果您对它没有完全的偏见感到满意,则应该没事。

But, if that's the case, why aren't you using uniqid ? 但是,如果是这样,为什么不使用uniqid呢?

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

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