Last night I was reviewing a new site that randomizes pages and I kept seeing the same pages coming up repeatedly. This is a rather common problem with many basic randomizers that do not keep track of what has already been shown.

This begs the question: how do you setup even distribution of a randomized pool of content? There are a couple different ways to do this, some of which are extremely efficient, but today we’ll review one that is very efficient and scales nicely.
First we need to pre-randomize the results and save an index of where we are at in the randomized pool. For example let’s say you have 10 numbers you want to randomize like this:
Now you would just need to remember the position of the randomized array from page to page so that the next randomized item in the array is displayed. To do this within the same browsing session we coud use sessions. To do this across browser sessions, we could use cookies. Here is some code illustrating how to do this using a session:
session_start();
if($_SESSION[‘index’] == 10 or $_SESSION[‘index’] == ”) {
// first or LAST time, generate numbers
$_SESSION[‘index’] = 0;
$rnumbers = range(1,10);
shuffle($rnumbers);
$_SESSION[‘numbers’] = $rnumbers;
}
$curindex = $_SESSION[‘index’];
$rnumbers = $_SESSION[‘numbers’];
print ‘Current randomize (#’ . ($curindex+1) . ‘): <b>’ . $rnumbers[$curindex] . ‘<hr />’;
$x = 1;
foreach($rnumbers as $each) {
if(($x-1) == $curindex) {
print “<font color=’green’><b>#$x: $each</b></font><br />”; // display all random numbers
} else {
print “#$x: $each<br />”; // display all random numbers
}
$x++;
}
$_SESSION[‘index’] = $curindex+1;
print ‘<hr />Refresh <a href="even_distribution.php">or click</a> for next randomized number. When all 10 numbers have cycled, a new shuffle of numbers in the pool will occur.’;
?>
Notes: start the code with session_start() to initialize the session. Then we check to see if the $_SESSION[’index’] is set or has reached the ceiling of the array. If the ceiling has been reached (10 numbers) then we reshuffle the array and reset the index to the first position.
This code also illustrates how to highlight the current item selected in a foreach loop in a bold green font. Since arrays begin numbering at zero but lists start numbering at 1, it is necessary to add and subtract one where applicable in the code. The $x variable is a list counter variable.
What if you want to add new content to the array?
Say you are currently at item #6 and there has been two new numbers added? You could wait until the current cycle is complete and re-randomize the numbers for that user or you could re-randomize the remaining indices with the new content added pushed to the end. You could then fill a new array with the remaining numbers, shuffle that and then replace the existing $numbers array starting at the next index number. This way the user would not see the content they’d already seen and would see the new content randomized into the list.
Above and beyond
This technique could be used for anything you could put into an array. If the array size is very big then you might want to work in stages, for example only load in 50 or 100 items into the session variables at a time as opposed to loading in thousands of possible pointers. Once the user has reached the random point, just grab a new randomized set of numbers from set #2 then set #3, etc all the way until all the pool has been exhausted.

Great idea, and it works nicely.
on January 25th, 2006 at 4:29 pm | #Link CommentLooks good, I want to use this to generate a range of shuffled numbers and then use them to reference an array with the locations of images to display a random image on my index page. however, I can’t get it to work properly.
I get this:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/reflecte/public_html/php1.php:9) in /home/reflecte/public_html/php1.php on line 10
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/reflecte/public_html/php1.php:9) in /home/reflecte/public_html/php1.php on line 10
am I supposed to put this code in the head section of the html page?
on March 20th, 2006 at 11:15 pm | #Link CommentGreate
It’s such a great site! http://ashantihwhoudgxt.gather.com Great post, I just bookmarked it on Digg.
on July 5th, 2011 at 2:35 am | #Link Commentreally good article
I have spent a bit of time going through your posts, more than I should have but I must say, http://www.thesingleshop.com/blogs/entry/The-Parties-With-Beauteous-Atramentous-Attire, many Thanks.
on August 7th, 2011 at 11:14 am | #Link Comment< b >< a href=”http://www.dedicatio.nl/wiki/JudithBckr” >bob hairstyles< /a >< /b >< /blockquote >
We are a group of volunteers and starting a new scheme in our community. Your web site provided us with valuable information to work on. You have done an impressive job and our entire community will be grateful to you.
on February 5th, 2012 at 10:16 pm | #Link Comment