Welcome to WebmasterWorld Guest from 54.196.144.242

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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)

Junior Member

10+ Year Member

joined:Nov 27, 2003
posts:159
votes: 0


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)

Junior Member

joined:Sept 9, 2003
posts:107
votes: 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)

Full Member

10+ Year Member

joined:Aug 21, 2000
posts:273
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 19, 2003
posts:89
votes: 0


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)

Junior Member

10+ Year Member

joined:Nov 27, 2003
posts:159
votes: 0


cheers all :)

Jon