Does array_rand use the Mersenne Twister algorithm

2019-07-18 10:14发布

In PHP, we have the choice of mt_rand() and rand() where mt_rand() uses the Mersenne Twister algorithm and rand() uses the libc random generator.

I would like to choose a random item out of an array using array_rand. However, does array_rand use the Mersenne Twister algorithm?

It is quite simple to implement my own array_rand using mt_rand, but if array_rand uses the Mersenne Twister, then all the better.

3条回答
趁早两清
2楼-- · 2019-07-18 10:50

It does now, as of PHP 7.1; see this RFC and this bug report for more details.

查看更多
ら.Afraid
3楼-- · 2019-07-18 10:54

It appears that array_rand uses php_rand() internally, which appears to be the good old non-mt version.

查看更多
萌系小妹纸
4楼-- · 2019-07-18 11:12

It was probably not the case since results from array_rand() used to be not random at all. It's been fixed in latest releases (see Vaxquis's answer for the details).

查看更多
登录 后发表回答