]> granicus.if.org Git - php/commitdiff
Test that mt_rand() consistently produces the wrong results
authorAndrea Faulds <ajf@ajf.me>
Thu, 18 Feb 2016 22:35:44 +0000 (22:35 +0000)
committerAndrea Faulds <ajf@ajf.me>
Thu, 18 Feb 2016 22:35:44 +0000 (22:35 +0000)
See 6f6bd8ce531636134efd5f669a4e8373fb2e9e51 and its revert,
a0724d30817600540946b41e40f4cfc2a0c30f80.

ext/standard/tests/math/mt_rand_value.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/math/mt_rand_value.phpt b/ext/standard/tests/math/mt_rand_value.phpt
new file mode 100644 (file)
index 0000000..772305a
--- /dev/null
@@ -0,0 +1,44 @@
+--TEST--
+Test mt_rand() output
+--FILE--
+<?php
+
+mt_srand(12345678);
+
+for ($i=0; $i<16; $i++) {
+    echo mt_rand().PHP_EOL;
+}
+echo PHP_EOL;
+
+$x = 0;
+for ($i=0; $i<1024; $i++) {
+    $x ^= mt_rand();
+}
+echo $x.PHP_EOL;
+
+/*
+ * Note that the output will be different from the original mt19937ar.c,
+ * because PHP's implementation contains a bug. Thus, this test actually
+ * checks to make sure that PHP's behaviour is wrong, but consistently so.
+ */
+
+?>
+--EXPECTF--
+1614640687
+1711027313
+857485497
+688176834
+1386682158
+412773096
+813703253
+898651287
+2087374214
+1382556330
+1640700129
+1863374167
+1324097651
+1923803667
+676334965
+853386222
+
+1571178311