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);
function randomizeArray ($data) {
$rarray = array();
foreach ($data as $i => $v) {
while ($data[$i][1] > 0) {
array_push($rarray, $data[$i][0]);
$data[$i][1]--;
}
}
$stat = 1;
while ($stat == '1') {
$stat = rarr($rarray);
}
return $stat;
}
function rarr ($a) {
$r = array();
$last = "";
while ($a) {
$rand = array_rand($a, 1);
if ($a[$rand] != $last) {
echo $a[$rand];
array_push($r, $a[$rand]);
$last = $a[$rand];
unset($a[$rand]);
} else {
$check = $a[$rand];
foreach ($a as $c) {
if ($check != $c) {
continue 2;
}
}
return 1;
}
}
return $r;
}