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

I have a simple code: 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.

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

Then the $F is used in a math calculation...

The $X value is the one remaining the same on every page refresh or revisit.

See in action @ Www.math-flashcards.com

Select "Numbers Plus 9" on second row of buttons. Input a name and then the code should be generating different flashcards each time.

Your code is only calling mt_rand() a single time. 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.

  • 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).