php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24909 rand function with range always returns low value of range
Submitted: 2003-08-01 14:37 UTC Modified: 2003-08-08 08:36 UTC
Votes:3
Avg. Score:3.3 ± 1.2
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:3 (100.0%)
From: a0 at hush dot com Assigned:
Status: Closed Package: Math related
PHP Version: 4.3.3RC3-dev OS: solaris 8
Private report: No CVE-ID:
 [2003-08-01 14:37 UTC] a0 at hush dot com
Description:
------------
when using the rand function with php 4.3.2 on solaris 8, it will
return a random value as expected if no range is specified.  if a range
is specified it will always return the low value of the range.

i'm building the module with -DZTS, and it is being loaded into
IWS4.1.

i've modified the code so it works by changing line 36 of
php_rand.h from
#if HAVE_LRAND48
to
#if (HAVE_LRAND48 && !sun)
so PHP_RAND_MAX will get a value of RAND_MAX.

Reproduce code:
---------------
<?
$r = rand();
echo "$r\n";
$r = rand(0, 100);
echo "$r\n";
?>


Expected result:
----------------
24384
0

(it will always return 0)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-02 12:46 UTC] iliaa@php.net
I've asked a number of people to replicate the problem, however they were unable to do so using latest PHP and Solaris 8 & 9. Please try the latest snapshot of 4.3.3 and see if the problem persists.
 [2003-08-02 13:55 UTC] a0 at hush dot com
thanks for the response.

i have downloaded 4.3.3RC2 and tried it.

first, i configured it using no args.  after i compiled
it, the rand() function behaves as expected, even with
a range specified.  this works.

i then configured it with --with-nsapi=/usr/local/iws41,
which is what we need to use the module with our
webserver.  after i compiled it, the cli program now
returns a 0 whenever a range is specified to rand().

i have not really looked through the code, but i would
guess this has something to do with the fact that php
is built with -DZTS in the second scenario (a different
random function is used in rand.c when this is
defined).
 [2003-08-02 15:00 UTC] iliaa@php.net
I've tried the ZTS version on both Linux & Windows and it worked perfectly in both cases. Did you run test via the nsapi module or using PHP cli or cgi binaries?
 [2003-08-02 15:17 UTC] a0 at hush dot com
i used the cli binary in both cases
 [2003-08-04 08:39 UTC] sniper@php.net
Please try this:

# 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";'


 [2003-08-04 12:18 UTC] a0 at hush dot com
i configured and built as you specified

Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

bash-2.05# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
16162
0
bash-2.05# sapi/cli/php -r 'echo rand(),"\n"; echo rand(0,100),"\n";'
31971
0
bash-2.05#
 [2003-08-07 15:48 UTC] thetaphi@php.net
This Test seems to be related to that:
=======================================================
==============
FAILED TEST SUMMARY
-------------------------------------------------------
--------------
Testing randomization of shuffle() and str_shuffle(). 
[ext/standard/tests/strings/004.phpt]
=======================================================
==============

You get it only in ZTS mode.
 [2003-08-08 08:36 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, 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/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 12:01:52 2014 UTC