简体   繁体   English

为什么mt_rand反复生成相同的数字?

[英]Why mt_rand generating same number repeatedly?

I have a simple code: mt_rand(1,25); 我有一个简单的代码:mt_rand(1,25);

it is generating the same number over and over. 它不断产生相同的数字。 MANY times before it will finally generate a new number. 最终将产生一个新数字很多倍。

6 months ago, it worked beautifully. 6个月前,它工作得很好。

    $X = mt_rand(1,25) ;
     $AR=array(14, 126, 28, .......);
     $F = $AR[$X];

Then the $F is used in a math calculation... 然后将$ F用于数学计算中...

The $X value is the one remaining the same on every page refresh or revisit. $ X值是在每次刷新或重新访问页面时保持不变的值。

See in action @ Www.math-flashcards.com 观看操作@ Www.math-flashcards.com

Select "Numbers Plus 9" on second row of buttons. 在第二行按钮上选择“数字加9”。 Input a name and then the code should be generating different flashcards each time. 输入名称,然后代码每次应生成不同的抽认卡。

Your code is only calling mt_rand() a single time. 您的代码仅一次调用mt_rand() If you are using this in a loop, please include the other statements in the loop. 如果您在循环中使用它,请在循环中包括其他语句。

Possible pitfalls: 可能的陷阱:

  • You are calling mt_srand() elsewhere in the loop, which is resetting the random state for each time through the loop. 您在循环中的其他位置调用mt_srand(),这会在循环中每次重置随机状态。

  • You are comparing successive executions of the script itself, and you have a PHP version that is out of date (all still-supported PHP versions initially seel the random state with a random value upon script execution). 您正在比较脚本本身的连续执行,并且您有一个过时的PHP版本(所有仍受支持的PHP版本最初在脚本执行时以随机值渗透随机状态)。

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

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