简体   繁体   English

如果mt_rand()同时运行,结果是否相同?

[英]If mt_rand() is run at the same moment, will the result be the same?

I am writing a PHP script to handle file upload to server. 我正在编写一个PHP脚本来处理文件上传到服务器。 To prevent overwriting when there are files with the same name, the program will rename each uploaded file to the current timestamp. 为了防止在文件名相同的情况下覆盖,程序会将每个上载的文件重命名为当前时间戳。

However, this is not enough. 但是,这还不够。 In peak hour, there may be files that are uploaded at the same second. 在高峰时段,可能会在同一秒上传文件。 To make sure the uploaded files will have different file names, I am thinking of adding a random number at the end of the timestamp. 为了确保上传的文件具有不同的文件名,我正在考虑在时间戳记末尾添加一个随机数。

Then, I read from the official PHP page on srand() 然后,我从srand()的官方PHP页面中阅读

Note: There is no need to seed the random number generator with srand() or mt_srand() as this is done automatically. 注意:无需使用srand()或mt_srand()为随机数生成器设置种子,因为这是自动完成的。

I guess they are using the timestamp for srand() . 我猜他们正在使用srand()的时间戳。 If 2 files are uploaded at the same second, so the timestamp for srand() , will the random result be the same? 如果在同一秒上传了2个文件,那么srand()的时间戳记,随机结果将是相同的吗? If yes, is there a way I can make sure the name is not duplicated even if they are uploaded at the same second? 如果是,是否有办法即使在同一时间上传名称也不会重复该名称?

PHP has some better random functions too, like random_bytes() and openssl_random_pseudo_bytes() . PHP也具有一些更好的随机函数,例如random_bytes()openssl_random_pseudo_bytes() These will return a guaranteed unique value. 这些将返回保证的唯一值。 Pass it to bin2hex() and you're all set! 将其传递给bin2hex() ,就一切bin2hex()

This is the code that generates the seeds 这是生成种子的代码

#define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
#else
#define GENERATE_SEED() (((zend_long) (time(0) * getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))

https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/standard/php_rand.h#L69 https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/standard/php_rand.h#L69

So it's a combination of timestamp and process id AND a psuedorandom 因此,它是时间戳和进程ID以及伪随机数的组合

https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/standard/lcg.c#L45 https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/standard/lcg.c#L45

combinedLCG() returns a pseudo random number in the range of (0, 1). CombinedLCG()返回(0,1)范围内的伪随机数。
The function combines two CGs with periods of 该功能将两个CG合并为
2^31 - 85 and 2^31 - 249. The period of this function 2 ^ 31-85和2 ^ 31-249。此功能的周期
is equal to the product of both primes. 等于两个素数的乘积。

So, i'd say you can rest assured they will not match. 因此,我想您可以放心,它们不会匹配。

No, seed will be used for random numbers range, not for single number. 不, seed将用于随机数范围,而不是单个数字。 Than you will be presented single number of that random-generated range. 比您将看到该随机生成范围的单个数字。 There is still possibility, that you will get same pseudo-random number. 您仍然有可能获得相同的伪随机数。

Try adding something like md5(time().$filename.'.'.$extension) instead. 尝试添加类似md5(time().$filename.'.'.$extension)

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

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