Re: com php-src: Fix #71152: mt_rand() returns the different values from original mt19937ar.c: ext/standard/rand.c
ext/standard/tests/math/mt_rand_value.phpt
| From: | Nikita Popov | Date: | Wed, 17 Feb 2016 09:57:03 +0000 |
| Subject: | Re: com php-src: Fix #71152: mt_rand() returns the different values from original mt19937ar.c: ext/standard/rand.c ext/standard/tests/math/mt_rand_value.phpt |
||
| References: | 1 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-91075@lists.php.net to get a copy of this message | ||
On Wed, Feb 17, 2016 at 4:32 AM, Xinchen Hui <laruence@php.net> wrote:
> Commit: 6f6bd8ce531636134efd5f669a4e8373fb2e9e51
> Author: kusano <kusano@users.noreply.github.com> Thu, 17 Dec
> 2015 19:26:38 +0900
> Committer: Xinchen Hui <laruence@gmail.com> Wed, 17 Feb 2016
> 11:32:59 +0800
> Parents: 837e2a912d1fb1b38438f0fe26362baf5b848467
> Branches: PHP-7.0 master
>
> Link:
>
> http://git.php.net/?p=php-src.git;a=commitdiff;h=6f6bd8ce531636134efd5f669a4e8373fb2e9e51
>
> Log:
> Fix #71152: mt_rand() returns the different values from original
> mt19937ar.c
>
> Bugs:
> https://bugs.php.net/71152
>
> Changed paths:
> M ext/standard/rand.c
> A ext/standard/tests/math/mt_rand_value.phpt
>
>
> Diff:
> diff --git a/ext/standard/rand.c b/ext/standard/rand.c
> index 2dd05e7..bc32f24 100644
> --- a/ext/standard/rand.c
> +++ b/ext/standard/rand.c
> @@ -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) ^
> ((php_uint32)(-(php_int32)(loBit(u))) & 0x9908b0dfU))
> +#define twist(m,u,v) (m ^ (mixBits(u,v)>>1) ^
> ((php_uint32)(-(php_int32)(loBit(v))) & 0x9908b0dfU))
>
> /* {{{ php_mt_initialize
> */
> diff --git a/ext/standard/tests/math/mt_rand_value.phpt
> b/ext/standard/tests/math/mt_rand_value.phpt
> new file mode 100644
> index 0000000..eeb009e
> --- /dev/null
> +++ b/ext/standard/tests/math/mt_rand_value.phpt
> @@ -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
>
Does this change the mt_rand sequence for all seeds or only specific seeds?
If the former, please do not apply it to PHP-7.0, it will break tests.
Also, if we're going to change the mt_rand sequence, we should also fix the
scaling at the same time.
Nikita