Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's array_rand() not really random?

Tags:

php

random

I am writing some code to randomize a list of presentations. The same presentation cannot play back to back.

The $d array would be a list of the presentation id’s and the number of times that presentation should play in a loop.

The code below works fine, but the results are not truly random. I was debugging when I started to see the same pattern over and over again. Look at this output:

ighbajafpbailgjacbiaeldiqjaphafgdjcbapsaebjfdkcknijhbdgecaimabodalkfbgbhacbhnrjeofbdjbfhegmbpdkialmbocnliaebfaimcabchgoecbcdimgepnfjgfbfbohdahdkjgneaebha

ighbajafpbailgjacbiaeldiqjaphafgdjcbapsaebjfdkcknijhbdgecaimabodalkfbgbhacbhnrjeofbdjbfhegmbpdkialmbocnliaebfaimcabchgoecbcdimgepnfjgfbfbohdahdkjgneaebha

These two were 4 runs apart. They’re identical. 4 runs later, the same identical string. 4 more runs, the same string. In fact, I seem to be getting the same 4 strings, in order, as I run this.

My code is below, can anyone point me to what I might have fubared? If I didn't fubar anything, why is "rand" so not random? How could I work around this?

$d = array(
    array('a', '20'), array('b', '20'), array('c', '10'), array('d', '10'), array('e', '10'), array('f', '10'), array('g', '10'), array('h', '10'), array('i', '10'), array('j', '10'), array('k', '5'), array('l', '5'), array('m', '5'), array('n', '5'), array('o', '5'), array('p', '5'), array('q', '1'), array('r', '1'), array('s', '1'));
$out = randomizeArray($d);
//var_dump($out);

function randomizeArray ($data) {
    // Step 1: Make an array of the id's with repeats for count.
    $rarray = array();
    foreach ($data as $i => $v) {
        while ($data[$i][1] > 0) {
            array_push($rarray, $data[$i][0]);
            $data[$i][1]--;
        }
    }
    // Step 2: Randomize.
    $stat = 1;
    while ($stat == '1') {
        //echo "<br><br>Randomizing Array: <br>";
        $stat = rarr($rarray);
    }
    return $stat;
}
function rarr ($a) {
    $r = array();
    $last = "";
    while ($a) {
        $rand = array_rand($a, 1);
        // Does this value match the last one we got?
        if ($a[$rand] != $last) {
            // Nope. Transfer to our $r array.
            echo $a[$rand];
            array_push($r, $a[$rand]);
            $last = $a[$rand];
            unset($a[$rand]);
        } else {
            // We have a match between our "last" presentation and the one we just selected.
            // We need to scan our array and verify that we still have different values.
            $check = $a[$rand];
            foreach ($a as $c) {
                if ($check != $c) {
                    // We have at least 2 different values in our array still.
                    // Rerun the while loop
                    continue 2;
                }
            }
            // All of the values left in our array are repeats.
            // That's no good, so return an error and rerun.
            return 1;
        }
    }
    // Everything is awesome. Return the randomized array.
    return $r;
}
like image 379
Jason Maggard Avatar asked May 07 '14 15:05

Jason Maggard


1 Answers

I was still interested why this function doesn't work properly. I've today tried to a bit and added

srand();

before launching randomizeArray function. I left code from question (so only array_rand) and added loop to test 10000 random strings.

It seems that it helped. Strings are now totally random, the same as using mt_rand.

I looked at documentation http://au1.php.net/manual/en/function.array-rand.php and there is here only info "4.2.0 The random number generator is seeded automatically.". But I also looked at Polish documentation at http://au1.php.net/manual/pl/function.array-rand.php - there is more info about it, translating it into English: "From PHP 4.2.0 there is no need to launch generator srand() or mt_srand() because it is done automatically"

Well, I was testing it on PHP 5.5.12 and in fact it seems it is not done automatically. I've also checked what happened if instead of srand() I will launch mt_srand() , both with or without seed. It seems it doesn't help at all. Only using srand() makes that array_rand is in fact random function.

like image 93
Marcin Nabiałek Avatar answered Nov 14 '22 03:11

Marcin Nabiałek