Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #71152: mt_rand() returns the different values from original mt19937ar.c #1681

Merged
merged 2 commits into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/standard/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ PHPAPI zend_long php_rand(void)
#define loBits(u) ((u) & 0x7FFFFFFFU) /* mask the highest bit of u */
#define mixBits(u, v) (hiBit(u)|loBits(v)) /* move hi bit of u to hi bit of v */

#define twist(m,u,v) (m ^ (mixBits(u,v)>>1) ^ ((uint32_t)(-(int32_t)(loBit(u))) & 0x9908b0dfU))
#define twist(m,u,v) (m ^ (mixBits(u,v)>>1) ^ ((uint32_t)(-(int32_t)(loBit(v))) & 0x9908b0dfU))

/* {{{ php_mt_initialize
*/
Expand Down
53 changes: 53 additions & 0 deletions ext/standard/tests/math/mt_rand_value.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
Test mt_rand() - returns the exact same values as mt19937ar.c
--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;

/*
excpect values are obtained from original mt19937ar.c as follows:

int i, x;
init_genrand(12345678);
for (i=0; i<16; i++) {
printf("%d\n", genrand_int31());
}
printf("\n");
x = 0;
for (i=0; i<1024; i++) {
x ^= genrand_int31();
}
printf("%d\n", x);
*/
?>
--EXPECTF--
527860569
1711027313
1280820687
688176834
770499160
412773096
813703253
898651287
52508912
757323740
511765911
274407457
833082629
1923803667
1461450755
1301698200

1612214270