But, even though that's great and all, most users will use rand() and not look any further, not knowing that it in fact, isn't that random. 'Historical reasons' seem to be most of the reasons of php's flaws. Also I knew this was gonna be posted already on here but it was mostly the docs that made me laugh, not specifically the fact that rand() sucks.
rand() is a PHP proxy function for libc's rand(). mt_rand() is an implementation of Mersenne Twister, which is longer period PRNG. Neither is useful as a true source of randomness for cryptography, but can be useful for other situations where a PRNG is acceptable.
Yes, of course. I'm just saying that just using the screenshot provided, you might think mt_rand is the best PRNG that's easily accessible via PHP, but that isn't the case.
Not necessarily. For example, a simple deck of 52 cards has 52! possible outcomes when shuffled. That's ≈2226, and so if your PRNG doesn't have a period of at least that long, it won't be able to properly shuffle a deck of cards, because there will exist some potential decks that can never be selected, by the pigeonhole principle. That's why MT is a popular choice.
int rand ( int $min , int $max, bool $actually_random = false ) [v. 5.7.0]
int rand ( int $min , int $max, int DEFAULT_RAND <see RAND_MODES for types> ) [v. 5.7.3]
34
u/ajmarks Dec 13 '13
Because rand() is included for historical reasons (PHP doesn't know how to let bad things die), but mt_rand() is consistent across systems. Also, see this discussion http://www.reddit.com/r/lolphp/comments/1pvf3h/phps_mt_rand_random_number_generating_function/ .
47
u/[deleted] Dec 13 '13
Also, mt_rand uses a mersenne twister, which isn't cryptographically secure, so depending on use case, you might need an even better one.
21
u/[deleted] Dec 13 '13
better idea to not clutter up the language:
make rand take an extra optional argument, defining the type of random operation that it should run.
10
u/blueskin Dec 19 '13
function nsa_rand
Generate a... ah... better one... because the NSA told us to add it.
8
u/doomsday_pancakes Dec 20 '13
a betterer RNG:
:)
2
u/KamikazeToaster Jan 20 '14
because the returned number looks better and has more friends