Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random items from an array - no repeats

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
I'm hoping to create a random function that will display a random order of the items in an array each time a page is called. The problem is that as each position in this array is used, the next random call should not access it so there are no repeats of the same item.

Here is the reckless code I have crafted so far:

$list = array ("apple", "orange", "banana","plum");
$itemzero = rand(0,3);
$zerofruit = $list[$itemzero];
$itemone = rand(0,3);
$firstfruit = $list[$itemone];
$itemtwo = rand(0,3);
$secondfruit = $list[$itemtwo];
$itemthree = rand(0,3);
$thirdfruit = $list[$itemthree];

print (&quot;$itemzero<br>$itemone<br>$itemtwo<br>$itemthree\n&quot;);

Is there a way to do this without randomly repeating items?

What if I have an array of 20 items? Can my sloppy code be simplified?
 
DOH - The output should have been...

print (&quot;$zerofruit<br>$firstfruit<br>$secondfruit<br>$thirdfruit\n&quot;);
 
How about a way to generate a set of random array indeces in the range of 0 through count(<your array>) - 1?

The function random_indeces accepts as in put your array of elements. It then returns the array I described above.

I have two versions of the generator in the code. The first (which is commented out) is good for arrays of small size. But as the arrays get large, the time the function takes gets big quick.

The second generator will randomly pick a point in the array. If that point has already been picked, it picks the next available higher array index, and wraps back around to zero when it gets to the end of the array. It may not, however, be as random as the first.

[tt]
<?php
function random_indeces ($arr)
{
$retval = array();

$count_of_assigned = 0;
$max_array_index = count($arr) - 1;

$scratch = array();
for ($counter = 0; $counter <= $max_array_index; $counter++)
{
array_push ($scratch, 0);
}

while ($count_of_assigned < count($arr))
{
$index = mt_rand (0, $max_array_index);

/* this will work for small arrays, but will get very slow for large arrays */
//if ($scratch[$index] == 0)
//{
// array_push ($retval, $index);
// $count_of_assigned ++;
// $scratch[$index] = 1;
//}

/* this will work for any array, and takes linear time per count of the input array.
But it may not be as random */

while ($scratch[$index] == 1)
{
$index ++;
$index %= count($arr);
}

array_push ($retval, $index);
$count_of_assigned ++;
$scratch[$index] = 1;
}

return $retval;
}


$foo = array (&quot;apple&quot;, &quot;orange&quot;, &quot;banana&quot;, &quot;plum&quot;);

print &quot;<html><body>&quot;;
print &quot;FOO:<br>&quot;;
foreach ($foo as $fruit)
{
print &quot;$fruit<br>&quot;;
}

$bar = array ();

$bar = random_indeces ($foo);

print &quot;<br>Random FOO<br>&quot;;

$index = array_shift ($bar);
while ($index !== NULL)
{
print &quot;$foo[$index]<br>&quot;;
$index = array_shift($bar);
}

print &quot;</body></html>&quot;;
?>
[/tt] ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
shuffle($list); //seems to work also

The PHP docs seem to warn about problems with 'shuffle' but it is working in my situation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top