PHP array_rand () is not really random?

I am writing code to randomize a presentation list. The same presentation cannot be played back.

The array $dwill be a list of presentation identifiers and the number of times the presentation should play in a loop.

The code below works fine, but the results are not random. I debugged when I reviewed the same template over and over again. Look at this output:

ighbajafpbailgjacbiaeldiqjaphafgdjcbapsaebjfdkcknijhbdgecaimabodalkfbgbhacbhnrjeofbdjbfhegmbpdkialmbocnliaebfaimcabchgoecbcdimgepnfjgfbfbohdahdkjgneaebha

ighbajafpbailgjacbiaeldiqjaphafgdjcbapsaebjfdkcknijhbdgecaimabodalkfbgbhacbhnrjeofbdjbfhegmbpdkialmbocnliaebfaimcabchgoecbcdimgepnfjgfbfbohdahdkjgneaebha

These two were scattered. They are identical. 4 starts later, the same line. 4 more runs, the same line. Actually, I seem to get the same 4 lines so that when I run this.

My code is below, can someone tell me that I could fubared? If I didn’t scare anything, why is the “rand” not so random? How can I get 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 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 no good, so return an error and rerun.
            return 1;
        }
    }
    // Everything is awesome. Return the randomized array.
    return $r;
}
+4
3

, , 2:

shuffle($rarray);

, . 47 .

rarr:

while ($a) {
    shuffle($a);

52

, array_rand. array_rand mt_rand, :

<?php
$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'));

    for ($i=0; $i<10000; ++$i) {
        //ho "x";
        $out = randomizeArray($d);
        echo implode('',$out)."<br />";   
    }    


//var_dump($out);

function randomizeArray ($data) {
    // Step 1: Make an array of the id 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 = mt_rand(0,count($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]);
            $a = array_values($a);
        } 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 no good, so return an error and rerun.
            return 1;
        }
    }
    // Everything is awesome. Return the randomized array.
    return $r;
}

.

, - , PHP ""

+2

, .

srand();

randomizeArray. ( array_rand) 10000 .

, . , mt_rand.

http://au1.php.net/manual/en/function.array-rand.php, "4.2.0 .". http://au1.php.net/manual/pl/function.array-rand.php - , : " PHP 4.2.0 srand() mt_srand(), "

, PHP 5.5.12, , . , , srand() mt_srand(), , . , . srand() array_rand .

0

rand () and array_rand () are pseudo-random functions

0
source

Source: https://habr.com/ru/post/1539604/


All Articles