ãã®è©±ã
PHPã®mt_rand()ãå®è£ ã«ãã¹ããããã¨ãç¥ã£ãã®ã§ããPHPã®ã³ããããã°ã«ååãè¼ãããâ( ï½¥ã ï½¥)Ùãã¨æã£ã¦ãã«ãªã¯ãéã£ãããä¸æ¦ãã¼ã¸ããããã©ããªãã¼ããããã
詳細
https://github.com/php/php-src/pull/1681/files
ã¤ãã§ã«ãã¹ãã³ã¼ããä»ãããã©ãç´ãã¹ãã¯1æåã ãã
twistã¨ãããã¯ãã®å®ç¾©ã1æåééãã¦ããã
loBit(u)
ã§ã¯ãªãloBit(v)
ãæ£ããã
#define twist(m,u,v) (m ^ (mixBits(u,v)>>1) ^ ((uint32_t)(-(int32_t)(loBit(u))) & 0x9908b0dfU))
ãã®ãã¯ãã¯ãªãªã¸ãã«ã®ã½ã¼ã¹ã³ã¼ãã®
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
ã¨ããå¦çã«å¯¾å¿ãã¦ãããæé©åããã¦ãã¦åããã¥ãããã©ãloBit(x)
ã¯0
ã1
ãè¿ãã®ã§ã符å·ãå転ãããã¨ã0x00000000
ã0xffffffff
ã«ãªããandãåããã¨ã§0x00000000
ã¨0x9908b0df
ã«ãªãããªãªã¸ãã«ã®ã½ã¼ã¹ã³ã¼ãã®mag01
ã¯{0x0UL, 0x9908b0df}
ãªã®ã§ã表ãå¼ããã«åãåä½ãããããã«ãªã£ã¦ããããªãªã¸ãã«ã¨åãçµæãå¾ãããã«ã¯ãu
ã§ã¯ãªãv
ã®ä¸ä½1bitã使ããªããã°ãããªãã
PHPã«mt_randãå®è£ ãããå½åããééã£ã¦ããããã§ã¯ãªãã10å¹´åã«ã³ã¼ããæé©åããã¨ãã«ã¨ã³ãã°ãããããã ãã®åã¯ãªãªã¸ãã«ã®ã¡ã«ã»ã³ãã»ãã¤ã¹ã¿ã¨åãå¤ãè¿ãã¦ããã¯ãã
æ®éã«ä¹±æ°ã使ãåã«ã¯å ¨ãåé¡ã®ç¡ãã¬ãã«ã ã¨æããã©ããã®éãã§ä¹±æ°ã®å¨ææ§ãªã©ãã©ã®ç¨åº¦æªåãã¦ããã®ãããããã¯å ¨ãå½±é¿ã¯ç¡ãã®ããæ°ã«ãªãã
çµç·¯
2015å¹´10æãã¨ããã»ãã¥ãªãã£ç³»ã®ã³ã³ãã¹ãã§mt_randãæ¨æ¸¬ããåé¡ã解ãã¦ãã¦ããªããC++ã§ã®å®è£
ã¨ä¸æãçµæãåããªãã£ããä¹±æ°ãæ¨æ¸¬ãããã¼ã«ãããã¨æãã¦ããã£ã¦ãã®ãã¼ã«ãè¦ã¦ããããã½ã¼ã¹ã³ã¼ãã®ä¸ã«mt_twist
ã¨ããé¢æ°ã¨php_twist
ã¨ããé¢æ°ããã£ã¦ãPHPã¯å®è£
ãç°ãªã£ã¦ãããã¨ãç¥ã£ãããªã®ã§ããã®ãã°ã¯ç§ãè¦ã¤ããããã§ã¯ãªããç¥ã£ã¦ãã人ã¯ç¥ã£ã¦ãã話ã ã£ããããã1æåã ãã ããç´ãããã¨ãæã£ããã©ãé¢åãããªã®ã§æ¢ããã
æåOSSã«ãã«ãªã¯ã ï¼ã¨æã£ããã©ãé¢åã ããæ¢ããã
— kusanoãã@ããã°ããªã (@kusano_k) 2015, 10æ 19
2015å¹´12æããã®ã¤ã³ã¿ãã¥ã¼ãèªãã§ãPHPããããªã¼ãã¨æããã³ããããã°ã«ååãæ®ããããªã£ãã®ã§ããã°å ±åããã¦ããã«ãªã¯ãéã£ãããã®æç¹ã§ãããããã«ãã°ã ãã©ãç´ãããäºææ§ãå´©ããã®ã§ã¯ï¼ãã©ãããããã¨ãããããªã³ã¡ã³ããã¤ãã¦ããã
2016å¹´2æããã®ã³ãããã¨è¡çªãã¦ããã®ã§ã解æ¶ãã¦ãã¤ãã§ã«ãããããã10å¹´åã®ä¿®æ£ã§äºææ§ãå´©ãã¦ãããã©ãä»ã¾ã§èª°ãæå¥ãè¨ã£ã¦ããªãããããä¸åº¦å´©ãã¦ãå¥ã«è¯ãã®ã§ã¯ãã¨ã³ã¡ã³ããä»ããã mt_rand()ã®è¿ãå¤ãå¤ãã£ããå°ããããªç¶æ³ãããã®ãªãã10å¹´åã®å¤æ´ã®æç¹ã§æ°ãä»ããã¦ãããã«ç´ãããã®ã§ã¯ãªããã¨ã ä»ã«ãã¦æãã¨ãã¡ãã£ã¨ä¹±æ´ã ã£ããããããªãã ãã£ã¨ãã¼ã¸ããããã¨æããããç´å¾ã«ãªãã¼ããããã
PHPã«éã£ã1æåä¿®æ£ãããã«ãªã¯ã¨ã¹ãããã¼ã¸ãããð mt_rand()ã®è¿ãå¤ãå ã®ã¡ã«ã»ã³ããã¤ã¹ã¿ã¨ç°ãªã£ã¦ãããhttps://t.co/Z5WJhHVyNd
— kusanoãã@ããã°ããªã (@kusano_k) 2016, 2æ 17
ãã®åãã¼ã¸ãããPHPã®mt_rand()ãä¿®æ£ããã³ãããããªãã¼ãããã¦ãã(´・Ïï½¥ï½)ç¥ããããªhttps://t.co/zKI1NlT46thttps://t.co/wZldV1Aqiv
— kusanoãã@ããã°ããªã (@kusano_k) 2016, 2æ 18
PHPã®ãã°ä¿®æ£ã®æ¹æ³
ãã®æã®å¤§è¦æ¨¡ãªã½ããã¦ã§ã¢ã¯éçºç°å¢ãæ´ããã ãã§ä¸è¦å´ã¨ããã¤ã¡ã¼ã¸ããã£ããã©ãã¨ã¦ãç°¡åã ã£ãã
git clone [email protected]:php/php-src.git cd php-src ./buildconf ./configure make
ã§ãsapi/cli/php
ã«æ¬ä½ãã§ãããä»ã®ãã¡ã¤ã«ãåèã«ãã¦ãtests/
ã«ãã¹ãç¨ã®ã¹ã¯ãªãããç½®ãã¨ãmake test
ã§å®è¡ãããã
README.mdã«æ¸ãã¦ããããã«ã https://bugs.php.net ã«ãã±ãããä½ã£ã¦ããã®çªå·ãåç §ãã¦ãã«ãªã¯ãéãã°è¯ããããã
ãªãã¼ãã«ã¤ãã¦
ãã ããPHPã¯ãã¡ãªãã ãã¨ãããããªæãã§å©ããã¦ãããããªãã¼ããããã¨ãã¯ããã£ãããã¼ã¸ãããã®ã«ãªãã¼ããããªãâ¦â¦ãã¨æã£ããã©ãæ£ãããããã¦ã¼ã¶ã¼ã®å©ä¾¿æ§ãåªå ãã¦ãããããããPHPã¯ããã¾ã§æµè¡ã£ã¦ããã®ã§ã¯ãªããã¨æãã ç®èã§ã¯ãªãã ãã¨ãä¹±æ°ã®å¨æã2100ã«ãªã£ã¦ãã¦ãå°ã人ã¯ããªããã©ãä¹±æ°ã®å¤ãå¤ãã£ããå°ã人ãªããããã
ç°å¢éä¾åã®ä¹±æ°
åã«æ§è½ã®è¯ãä¹±æ°ãªãã¢ã«ã´ãªãºã ã®ååãä»ããå¿ è¦ã¯ç¡ãã®ã§ãåè¨èªã«å®è£ ããã¦ããã¡ã«ã»ã³ãã»ãã¤ã¹ã¿ã¯è¨èªãå¤ãã£ã¦ãåç¾æ§ã®ããä¹±æ°ãå¾ãããã®ãé åã ã¨æã£ã¦ãããä¾ãã°ãä»åã®ä¿®æ£ãåæ ãããã°ã
<?php mt_srand(12345678); for ($i=0; $i<8; $i++) echo mt_rand().PHP_EOL;
ã¨
#include <iostream> #include <random> int main() { std::mt19937 rand(12345678); for (int i=0; i<8; i++) std::cout<<rand()/2<<std::endl; }
ã¯ã©ã¡ãã
527860569 1711027313 1280820687 688176834 770499160 412773096 813703253 898651287
ã¨ããå¤ãåºåããããã«ãªããC++ã¯32bitã§å¤ãè¿ãã®ã§ã/2
ãã¦ããã®ãã¡ãã£ã¨æ®å¿µã ãã©ã
ä»ã®è¨èªã¯ã©ããªã®ãã¨ãPythonãè¦ã¦ã¿ãã¨ãgenrand_int32()
ã®å¤ãç´æ¥åå¾ããã«ã¯getrandbits(32)
ã¨ããç¡çç¢çæã®ããæ¹æ³ã ããinit_genrand()
ãå¼ã³åºãæ段ã¯ç¡ãããã ã£ãã
è¨èªéã§ãåç¾æ§ã®ããä¹±æ°ã¨ããéè¦ã¯ãã¾ãç¡ãã®ãããããªããæ®å¿µã
ãã¾ã
ç§ããã°ãã±ãããä½ã£ãã¨ãã«ç´åã«ãã£ããã°ãé¢ç½ãã£ãã
https://bugs.php.net/bug.php?id=71151
<?php
ã ãã®ãã¡ã¤ã«ã¯HTMLããã®è±åºãã§ããªããããããphp
ã®å¾ã«ã¯ç©ºç½æåãå¿
è¦ã§ãEOFã¯ç©ºç½æåãªã®ãã¨ããå²å¦çãªåé¡ã ãã¨Rasmusæ°ãã³ã¡ã³ããã¦ããã
追è¨
phpã®mt_rand()ã®ä»¶ã«ã¤ãã¦Revertããæ¹ã®ã¢ã³ãµã¼ã½ã³ã°ã®ããã§ããã確èªãã ãããhttps://t.co/kgW5Tnnu68https://t.co/FYmMWacU6s
— sasezaki (@sasezaki) 2016å¹´2æ27æ¥
PHPã§ã®ãã¡ã«ã»ã³ããã¤ã¹ã¿ã¨ä»ã®mt_rand()ã¨ç価ãªå¦çã®å®è£ ãä¹±æ°ã®å¤ãä¸è´ããå¿ è¦ãããå ´åã«ã¯ããã使ãã°è¯ãããã
lt/PHP-MT19937: PHP implementations of MT19937, MT19937-64 and PHPs MT variant