Welcome to WebmasterWorld Guest from 54.167.177.207

Forum Moderators: coopster & jatar k

Random Variable Echo

I want to use an array to print some words in varying order.

   
1:16 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



Hi,

I need help. I want to use 'mt_rand()', to print a series of links in different order. I thought about using an array.

How can I do this so the same link isnt printed twice, or some of the links don't appear.

It's so I can produce a list of links in random order so nobody gets missed out.

Any idea how?

Jon

1:44 pm on Jan 19, 2004 (gmt 0)



Hey try this out. Just remember to that arrays start their count at 0 so if you add 10 random links into the array then your rand function should be rand(0,9)

$links= array("www.site1.com","www.site2.com","www.site3.com");
$linkT=array("site1","site2","site3");
$r=rand(0,2);
<a href="<? echo ("http://$links[$r]");?>"><? echo $linkT[$r];?></a>

1:54 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



If you want to print the entire list of links randomly, look at using array_rand() to randomize your array before printing it out.
3:36 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



Actually using shuffle() might be a better idea. All you would need to do is to seed the random number generator, shuffle the array, then dump the entire array.

function dump_rand_links ($links) {
srand((float)microtime() * 1000000);
shuffle($links);
foreach ($links as $link) {
echo "<a href=\"{$link}\">{$link}</a><br />";
}
}

This way you don't need to worry about links being missed or duplicated.
12:16 am on Jan 21, 2004 (gmt 0)

10+ Year Member



cheers all :)

Jon

 

Featured Threads

Hot Threads This Week

Hot Threads This Month