...

View Full Version : Two randomized outputs



ssskaya
02-03-2007, 10:48 PM
I use the simple code below to generate random outputs.


<?php
$ads = array(
'item1','item2','item3');
$rand = rand(0,2);
echo $ads[$rand];
?>

However, I need to make a simple advancement.

I need to generate two random outputs instead of one - like publishing two quotes/pictures/whatever next to each other at the same time.

How can I do that?

Thank you.

firepages
02-04-2007, 02:31 AM
just call rand twice ..


<?php
$ads = array('item1','item2','item3');
$max=count($ads)-1;

echo $ads[rand(0,$max)];
echo $ads[rand(0,$max)];

?>

Mwnciau
02-04-2007, 02:37 AM
If you wanted them not to be the same you could do something like:



<?php
$array = array(...);

$rand1 = mt_rand(0, sizeof($array)-1);
$rand2 = mt_rand(0, sizeof($array)-1);

while ($rand1 == $rand2){
$rand1 = mt_rand(0, sizeof($array)-1);
$rand2 = mt_rand(0, sizeof($array)-1);
}

echo $array[$rand1] . $array[$rand2];
?>

ssskaya
02-04-2007, 02:49 AM
Thanks! Works great!

ralph l mayo
02-04-2007, 03:41 AM
array_rand (http://us3.php.net/array_rand)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum