dmorg Posted March 6, 2007 Share Posted March 6, 2007 Hi All, Am I right in assuming that if I seed mt_rand() with the same number, I should get the same sequence of pseudo-random numbers? This assumption has worked for me in the past but seems to generate a different sequence with 5.2.1 <?php mt_srand(42); echo mt_rand(); ?> produces 1387371436 under 4.*, 5.1.1. 5.1.4 produces 1354439493 under 5.2.1 I have some student assignment questions written in php, seeded by the student id number so that each time the student logs in, he/she gets the same input variables to questions (so they can pick up where they left off). My host server upgraded to 5.2.1 a few days ago and students can no longer reproduce previous assignment inputs. So, should php produce the same sequence when seeded with the same value, across different versions of php? Shouldn't the algorithm be consistent? Or is there some security reason for the recent change? Any insights will be appreciated. (You can check out an assignment at http://yamnuska.ca/student/smtl/assigns/A1/login.php Any names and number will get you in) Thanks, Dave Quote Link to comment Share on other sites More sharing options...
dmorg Posted March 6, 2007 Author Share Posted March 6, 2007 With information from a very helpful CPSC lecturer (with his own very fast implementation of the Mersenne Twister), it seems that php4.4.6 uses Shawn Cokus' implementation and php 5.2.1 use an implementation by Richard Wagner. In Jan 02, Matsumoto and Nishimura changed their reference implementation of the random seeding function; Wagner uses the new implementation and Cokus uses the old. Other implementations keep both implementations for backwards compatibility; unfortunately, php 5.2.1 does not. Developers whose code depends upon a reproducible sequence of pseudo random numbers will find that previous sequences are no longer available. On the bright side, this is unlikely to change again in the near future. Dave Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.