PHP Randomizer
March 8, 2004 6:54 PM   Subscribe

PHP-randomizer help?

I'm using this script to present a random string on a page. It works well, and I like that I can use the file that contains the strings for other purposes (I can use the file as is, there isn't any PHP in it).

My problem is I'm greedy, and I want *3* random, non-repeating strings written to the page.

The file I'm using is that has an option to run through the strings sequentially (using an additional file to store the last displayed item), which I thought was the key to making it work (just call the script 3 times, it should give you 1, 2, and 3), but it's not working.

Can anyone point me to a script that will do what I want? I want to use a source file to print multiple, non-repeating, random (or sequential) strings of HTML?
posted by o2b to Computers & Internet (10 answers total)
 
So you're looking for a PHP script that will print out 3 random lines from a text file in one go? I'm just looking for a clarification because I don't quite get what you're asking.
posted by crawl at 7:15 PM on March 8, 2004


<?php
$quotes = file('filename.txt');
$quote_numbers = array();
srand((float) microtime() * 10000000);

while(sizeof($quote_numbers) < 3) {
$random = rand(0, sizeof($quotes) - 1);
if(!in_array($random, $quote_numbers)) $quote_numbers[sizeof($quote_numbers)] = $random;
}

foreach($quote_numbers as $number) {
echo $quotes[$number], "<br>\n";
}
?>

The effectiveness of this script relies on the version of PHP installed on your server. In rather old PHP installations, the in_array() function is unsupported. Post-4.2.0, you can get rid of the line that begins with srand, as the random number generator is seeded automatically.
posted by Danelope at 7:21 PM on March 8, 2004


Here's what I have for ya, based on the above assumption(s):
<?php
$filename = '/path/to/textfile.txt';
if ($fileContents = file_get_contents($filename)) {
$contentsArray = explode("\n", $fileContents);

$numLines = count($contentsArray)-1;
print($contentsArray[rand(0,$numLines)] . "<br />\n"); // 1
print($contentsArray[rand(0,$numLines)] . "<br />\n"); // 2
print($contentsArray[rand(0,$numLines)] . "<br />\n"); // 3
}
else {
die('Could not get contents of: ' . $filename);
}
?>

On preview: Stupid tabs disappearing.
posted by crawl at 7:25 PM on March 8, 2004


Hey, I didn't know about the file() function, Danelope. Cool.
posted by crawl at 7:26 PM on March 8, 2004


Ahh, but you have shamed me with your dilligent error checking and XHTML-standard markup, crawl. I suppose we're even!
posted by Danelope at 7:31 PM on March 8, 2004


Response by poster: I am no match for your PHP skills. I have taken what I think is the best of both and it works very nicely.

crawl: the tabs re-appeared upon cut and paste.
posted by o2b at 7:48 PM on March 8, 2004


Response by poster: Thanks!
posted by o2b at 7:48 PM on March 8, 2004


Also check out array_rand(). Sorry, I have not the wakefulness necessary to try and build you a block of code to work with.
posted by brownpau at 11:19 PM on March 8, 2004


The PHP docs doesn't say; does array_rand() produce unique values, or would you have to throw an array_unique() into a while() loop until the target array size is reached?

(I suppose I could just test it...)
posted by Danelope at 8:08 AM on March 9, 2004


I'm using it for a randomizing blogroll, and I've never seen repeated entries come up.
posted by brownpau at 10:42 AM on March 9, 2004


« Older Coming down is hard to do   |   How to import updated bookmarks from IE to Firefox... Newer »
This thread is closed to new comments.