Hi,
I am currently working on a CGI / database search that will be display 5 results per page, and they have to be "random". Actually this just means that each item must have an equal opportunity to appear first in each new search. I have accomplished this easily enough by rearanging the results based on a random number produced each time, then, sending the number to the next page where the db is queried again, so the next page's "random" results are exactly the same as the first, only this time I show the next 5.
my $total_count; #The total number of items returned by the search
my $newcount = ($total_count - 1);
my $ORDER; #If this is not the first page, this is the original ran
+dom number keeping the order between pages
if (!$ORDER) { $random_num = int(rand($total_count)) + 1; }
else { $random_num = $ORDER; }
for ($z = 1; $z < $total_count; $z++) {
my $random = $random_num;
while ($newcount < $random) {
$random = ($random - $newcount);
}
for ($a = 0; $a <= $count; $a++) {
if ($a < $random) { push(@front,$nonrandom[$a]);
}
if ($a == $random) { push(@final,$nonrandom[$a]);
}
if ($a < $random) { push(@back,$nonrandom[$a]);
}
}
@nonrandom = (@back, @front);
@back = ();
@front = ();
$count--;
}
push (@final,$nonrandom[$a]);
$thispage = $pagenumber * 5;
for ($a = ($thispage - 1); a < ($thispage + 4); $a++) {
$thispageresults = $final[$a];
}
Although my solution this time is fine this time, I am sure someone knows a cool way to get truly random results, and pass the same randomness between pages in a CGI script.
I first thought about saving the initial query results, which could be made truely random in a temporary file to be read at each subsiquent call needing that same sequence, but decided that it would be more trouble than it's worth to have to worry about how long the files should be saved, and what would happen if too many visitors did too many differnet searches (with thousands of items returned in the result) filling up my disk space with gigabytes of random replys.
Any ideas and advise would be great!
Kbeen.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.