|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #40724 mt_srand returns different results in 5.2.1 than in previous versions
Submitted: 2007-03-05 07:52 UTC Modified: 2007-06-15 01:08 UTC
Avg. Score:4.3 ± 0.7
Reproduced:5 of 6 (83.3%)
Same Version:4 (80.0%)
Same OS:3 (60.0%)
From: dave at dmorg dot org Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.1 OS: linux, windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
13 - 6 = ?
Subscribe to this entry?

 [2007-03-05 07:52 UTC] dave at dmorg dot org
I use srand to seed random numbers with student id numbers so that whenever students relog in to an assignment, their questions have the same input values and they can continue where they left off.

I am now getting different results than before. I'm not sure whether this is an unintended consequence of some apparently unrelated code change or whether it is intended. 

Reproduce code:

echo mt_rand();

produces 1387371436 under 4.*, 5.1.1. 5.1.4

produces 1354439493 under 5.2.1

Expected result:
I would expect results from the random number generator to be consistent  when seeded with the same number

Actual result:
produces 1387371436 under 4.*, 5.1.1. 5.1.4

produces 1354439493 under 5.2.1


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-05 23:57 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Random numbers are random, so you can't expect to get the same number 
out of a random number generator.
 [2007-03-21 14:36 UTC]
Except that you can, if you pass it the same seed. Mersenne Twister is still a psuedo-random system; same seed should generate the same output. This is a subtle but non-trivial change for people generating psuedo-cryptographic sequences.

(Ilia, don't tell me that MT isn't cryptographically secure, of course it's not - that's not the point).
 [2007-03-21 14:47 UTC]
>Mersenne Twister is still a psuedo-random system; 
>same seed should generate the same output.

And it does generate the same output.
Though nobody guranteed that in different PHP versions.
 [2007-03-21 15:59 UTC]
It's still a BC break. I know, from a maintainer's standpoint, how infinitely annoying small changes like this are. But this change breaks code, with no way to go back to the old behavior. The fact that no one promised that the random number generator wouldn't change is a false framing of the question - no one said that the random number generator _would_ change either. In fact the docs for mt_rand specifically encourage using it because, as compared to c library rand functions, it has "known characteristics".

I think this should be reverted or somehow the old algorithm should be made available, but at the very least this needs to have a changelog entry, and should be updated to note that you can't rely on mt_srand() taking a seed consistently between PHP versions. (I still think that defeats the "known characteristics" statement also).
 [2007-03-21 16:18 UTC]
>It's still a BC break.
Changing an error message from "Failed to get" to "Cannot get" is also a "BC break", but you should not rely on it in the first place. 
And note that noone said that error messages would change.

"Known characteristics" have nothing to do with returning the same value.
In 5.2.1 the mersenne twister implementation in PHP has been changed to use faster and better licensed code.
Unfortunately, Dmitry did not add a NEWS entry about it, though I really doubt it would help anybody.

Still no bug here, do not rely on these values.
 [2007-03-21 16:36 UTC] dave at dmorg dot org
When I reported this non-bug, I got the rather unhelpful (and, indeed, inaccurate) reply that "Random numbers are random, so you can't expect to get the same number out of a random number generator."

I did a bit of digging and got some generous feedback from Geoff Kuenning (who has an implementation of the MT seed generation algorithm).

Apparently, versions prior to 5.2.1 used an implementation written by Shawn Cokus. But, in 2002, Matsumoto and Nishimura changed their reference implementation of the random-seeding function (these are the brains that came up with the Mersenne Twister). Version 5.2.1 uses a newer implementation by Richard Wagner, which uses only the new method and generates a different sequence of pseudo-random numbers. Keunning's implementation, incidentally, "...kept both the old and new algorithms precisely to avoid the sort of problem that you have encountered." According to Keunning, we're unlikely to see another change in the algorithm so results from 5.2.1 should remain constant in future versions of php.

 [2007-03-26 16:48 UTC]
Something should be added to the documentations changelog
 [2007-06-15 01:08 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Dec 09 02:01:27 2023 UTC