4 ms·
For some added context, the normal rand function in PHP uses an implementation based on libc. PHP provides a second, safer, function, mt_rand, which is based a
by halo 17y ago
For some added context, the normal rand function in PHP uses an implementation based on libc.
PHP provides a second, safer, function, mt_rand, which is based around an implementation of the Mersenne Twister.
- deleted 17y ago[deleted]
- windsurfer 17y agoIt would make more sense to call mt_rand simply rand, and the current rand to fast_rand.
- zokier 17y agowell, that could make some sense, but mt_rand is significantly faster too than basic rand. edit: what I meant was that imho there is not really any reason to keep that old, broken rand function lying around
- masklinn 17y agoSo basically PHP keeps the slower and worst random generator as the default. Great minds in action.
- maneesh 17y agophp, now with goto!
- Scriptor 17y agoBackwards-compatibility should not be an issue here either. Is there any way to actually be dependent on the regularity of rand()?
- gojomo 17y agoYes. Some applications want series of numbers that are pseudorandom, but completely reproducible, once you've provided the same seed. (See PHP's srand().) This is often the case with simulations, or other algorithms that have a 'random' component that you'd still like to hold constant across (for example) related runs during testing or across distributed machines. So there may be a sensible case for maintaining backward-compatible rand() behavior here, even though a better algorithm is available.
- tesseract 17y agoThen make srand = mt_srand.
- gojomo 17y agoThat doesn't resolve the problem for code relying on stable behavior of these functions. Given the following code... srand(CONSTANT); n = rand(); ...the scenarios I mentioned benefit from n being the exact same number in PHP version X and then version X+1, X+2, etc. If PHP swaps out both rand() and srand(), backward-compatibility is broken, and along with it some kinds of quite-reasonable code that relied on srand() initializing rand() to give the same deterministic stream of numbers (rather than just a deterministic stream). It might still be worth taking that hit -- I don't have a strong opinion -- but there are good reasons not to break backwards-compatibility, even to improve PRNG quality.
- ecaron 17y agoThe difference when using mt_rand is astonishing. PHP 5.3 graph using rand: http://twitpic.com/gq81b http://twitpic.com/gq81b PHP 5.3 graph using mt_rand: http://twitpic.com/gq827 http://twitpic.com/gq827
- jacquesm 17y agoThe regular rand can do that just as well. see elsewhere in this thread.
- pyman 17y ago> before you criticize a project make sure you understand the material. He's right. Mersenne Twister = mt_rand(). The guy that wrote this is an ignorant. Ignorant: Lacking knowledge. Unaware or uninformed (adjective: moron).
- jacquesm 17y agoThe only thing that is left from this whole circus is a reference to a bug that has been known about for a long time: http://bugs.php.net/bug.php?id=45184 http://bugs.php.net/bug.php?id=45184 It would have really helped if they did a bit of research before writing such an inflammatory post and then go all out to get people to spread it. Anyway, the damage is done.
- tptacek 17y agomt_rand isn't crypto-safe either. You need to be using the system's secure random number generator, or OpenSSL's.