php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20793 big selection with array_rand() works bad EVEN with srand()
Submitted: 2002-12-03 11:21 UTC Modified: 2002-12-03 15:24 UTC
Votes:2
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dave at arthistory dot cc Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.2.3 OS: Linux (redhat6.2) 2.2.14-5.0smp
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dave at arthistory dot cc
New email:
PHP Version: OS:

 

 [2002-12-03 11:21 UTC] dave at arthistory dot cc
I use array_rand() to randomize an array that can have three different sizes, 450, 800 and 1152. With 450 array_rand works great. But when I try to select 800 and 1152 indexes with array_rand(), array_rand seems to seed higher number. I everycase I always get the 25 last indexes picked! Pretty weird! It seems to favor the last number!

I have tried:
	srand ((float) microtime() * 10000000);
	srand ((float) microtime() * 1000000);

But that didn't helped:

Here's my code:

if($bricks==1) $tempArrValue=450;
if($bricks==2) $tempArrValue=800;
if($bricks==3) $tempArrValue=1152;
	
$brickArray = array();
for($i=1;$i<=$tempArrValue;$i++){
	$brickArray[$i]=$i;	
}
srand ((float) microtime() * 10000000);
$brickArray2 = array_rand($brickArray, sizeof($brickArray)-3);
for($i=0;$i<sizeof($brickArray2);$i++){
	mysql_query("insert into puzzle_bricks values(0,$countryIndex,$i, " . $brickArray2[$i] . ");");
}

My conclusion is that their are some problems when seeding random numbers for array_rand when you are selecting an array with more than 800 indexes as output.

I mean I have solved the issue by making my own array_rand function, but I just wanted to help makeing PHP a better product.

Best Regards Dave,

Ps, I love PHP, Ds

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-03 11:24 UTC] dave at arthistory dot cc
http://www.arthistory.cc/info.php for my php info
 [2002-12-03 11:28 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip


 [2002-12-03 11:35 UTC] dave at arthistory dot cc
I will test the snapshot this weekend, I have no access to a test machine earlier

Ps, array_rand($brickArray, sizeof($brickArray)-3);
should be array_rand($brickArray, sizeof($brickArray));,
the error occur with both! Ds
 [2002-12-03 12:35 UTC] msopacua@php.net
Here's something to test properly:
<?php
function printline($string)
{
        static $nl = -1;
        if($nl == -1)
                $nl = (isset($_SERVER['REMOTE_ADDR'])) ? "<br />\n" : "\n";

        print("$string$nl");
}

$bricks = (isset($argv[1])) ? $argv[1] : 1;
if($bricks==1) $tempArrValue=450;
if($bricks==2) $tempArrValue=800;
if($bricks==3) $tempArrValue=1152;
        
$brickArray = array();
for($i=1;$i<=$tempArrValue;$i++){
        $brickArray[$i]=$i;     
}
srand ((float) microtime() * 10000000);
$brickArray2 = array_rand($brickArray, sizeof($brickArray));
for($i=0;$i<sizeof($brickArray2);$i++){
        printline("$i => {$brickArray2[$i]}");
}
?>

I can't reproduce it - in fact, the following quick-hack-shell-tester doesn't yield any output:
#!/bin/ksh

typeset -i COUNTER=1

while [ $COUNTER -lt 100 ]; do
        ./test_array_rand.php 3 | awk '{print $3}' | sort | perl -n -e 'print if $_ eq $previous; $previous=$_'
        COUNTER=`expr ${COUNTER} + 1`
done

You should probably look at the query, rather than at rand. But then again - if the above yields output, report back.
 [2002-12-03 15:24 UTC] dave at arthistory dot cc
I'm terrible sorry the cause is most certain mysqlbased, for some reason the first 100'rows contains almost 50% of number betweens 1000-1152 (from a random selection of 1-1152) weird.

But the with the script:
$tempArrValue=1000;
$brickArray = array();
for($i=1;$i<=$tempArrValue;$i++){
        $brickArray[$i]=$i;     
}
srand ((float) microtime() * 10000000);
$brickArray2 = array_rand($brickArray, sizeof($brickArray));
$temp = 0;
for($i=0;$i<=250;$i++){
$temp = $temp + $brickArray2[$i];
}
echo $temp / 250;

I did get a correct answer, recieved number around 500.

Again I'm terrible sorry for taking your time, please contact me of you want me to test something other, or anything else I can do.

Best Regards Dave
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 02:01:36 2025 UTC