Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Even PHP includes a good Mersenne Twister implementation (mt_rand() [1]) as part of the core library...

[1] http://www.php.net/manual/en/function.mt-rand.php



But why wouldn't they just use it to replace rand()?


Because mt_rand(), even when seeded with the same number, does not generate a predictable sequence (ie, it's a really good MT implementation). For those who do want a predictable RNG based on seed value (regardless of whether you agree with this usage), they can opt to use the libc implementation of rand() and srand().


When you say "predictable", do you mean "repeatable?" Or do you really mean "predictable?"

In the latter case, well, if you want predictable numbers you shouldn't be calling a RNG at all. And in the former, it's not good, it's broken, PRNGs are supposed to produce the same sequence, given the same seed....


The MT implementation used with Lua* generates a predictable sequence based on seeds, though. It may just be the PHP implementation that makes random number sequences non-reproducible. Why would this be desirable, by the way?

* LHF's lrandom: http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lrandom . While this is not in the core language, it's by one of Lua's three authors, and could be considered a semi-official extended library. (Since Lua is targeted for embedding, it's core standard library has been kept very tiny.)


Are you sure about that? PHP Manual (http://php.net/manual/en/function.mt-srand.php) does not say anything about that.

The manual does, however, note that since PHP 5.2.1 "Identical seeds no longer produce the same sequence of values they did in previous versions.", which some people in the manual comments section took to mean that identical seeds could produce a different sequences even in the current version, but this does not appear to be so.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: