Jump to content

new mt_srand() under 5.2.1?


dmorg

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.