php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26949 rand(min,max) always returns min when zts enabled
Submitted: 2004-01-17 17:34 UTC Modified: 2004-01-18 21:25 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: alucard at vitriolic dot org Assigned:
Status: Closed Package: Math related
PHP Version: 4CVS-2004-01-18 OS: Solaris 9
Private report: No CVE-ID:
 [2004-01-17 17:34 UTC] alucard at vitriolic dot org
Description:
------------
Bug #24909 is still an issue with latest build and Solaris 9.

When rand() is called with no arguments, it operates as expected.  When rand(min,max) is called, it always returns value 'min'.

This problem only occurs when ZTS is enabled.  When it's not, both calls to rand() work as expected, though the value ranges are completely different.

To demonstrate:

# uname -a
SunOS XXXXXX 5.9 Generic_112233-08 sun4u sparc SUNW,Ultra-Enterprise

# rm config.cache
# ./configure --disable-all --disable-cgi --enable-experimental-zts
# make clean && make
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
28909
0
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
21885
0

# rm config.cache
# ./configure --disable-all --disable-cgi
# make clean && make
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
1116498057
87
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
102735333
95

Note that with zts enabled, the rand() values are much smaller, and rand(min,max) always returns min.  Without it, rand values are much larger, and rand(min,max) operates as expected.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-18 02:04 UTC] alucard at vitriolic dot org
Same results:

# rm config.cache
config.cache: No such file or directory
# ./configure --disable-all --disable-cgi --enable-experimental-zts
# make clean && make
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
29622
0
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
6807
0
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
2050
0
# sapi/cli/php -v
PHP 4.3.5RC2-dev (cli) (built: Jan 18 2004 00:52:22)

Without zts enabled, the results are:
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
2121634529
48
# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
1303411959
20
 [2004-01-18 09:35 UTC] sniper@php.net
Try adding 'srand();' call there:

# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'

Does this work better?
(Note: I can not reproduce this within Linux)

 [2004-01-18 09:36 UTC] sniper@php.net
Also, what does this output (in zts and in non-zts mode):

# sapi/cli/php 'echo getrandmax();'



 [2004-01-18 16:16 UTC] alucard at vitriolic dot org
No worries, I don't mind helping you troubleshoot this one.  Not many folks run Solaris boxen as their home machines, can't imagine why...;)

Without ZTS:
# sapi/cli/php -r 'echo getrandmax(),"\n";'
2147483647
# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'
32
# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'
5
# 
# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'
22

With ZTS:
# sapi/cli/php -r 'echo getrandmax(),"\n";'
2147483647
# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'
0
# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'
0
# sapi/cli/php -r 'srand(); echo rand(0,100),"\n";'
0

The value returned by getrandmax() is definitely not what's being used by rand() with ZTS enabled.  Calling rand() approx. 150 times and sending the output to a file, the highest value I get is 32481.  This leads me to believe that despite what's being returned by getrandmax(), it's using 32768 as its max in operation.

mt_rand() works as expected with ZTS enabled, both with and without ranges specified:
# sapi/cli/php -r 'echo mt_rand(),"\n";'
1128343079
# sapi/cli/php -r 'echo mt_rand(),"\n";'
1820256748
# sapi/cli/php -r 'echo mt_rand(0,100),"\n";'
68
# sapi/cli/php -r 'echo mt_rand(0,100),"\n";'
83
# sapi/cli/php -r 'echo mt_rand(0,100),"\n";'
54

mt_getrandmax() returns the same result as getrandmax() (but, as shown above, appears to actually use it).
# sapi/cli/php -r 'echo mt_getrandmax(),"\n";'
2147483647
 [2004-01-18 21:25 UTC] sniper@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 15:02:26 2014 UTC