Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I am looking for a way to displ

Status
Not open for further replies.

kawnz

Technical User
Jan 30, 2003
67
US
Hi, I am looking for a way to display two random images from the same directory, on the same page, but of course, I want them to be different.

I looked for random numbers not the same, and found this snippet by skiflyer.

I can determine the number of files in the directory, but cannot plug them into the snippet here.

eg $images = sizeof($filelist)-1;

and I tried putting $max=$images in as follows

function random_nums($x=10, $min=0, $max=$images) {

but it won't work. Can you explain why?



<?

function random_nums($x=10, $min=0, $max=10000) {

$list_of_nums = array();
for($i=0;$i<$x;$i++) {
$temp=mt_rand($min,$max);
while(in_array($temp, $list_of_nums)) {
$temp=mt_rand($min,$max);
}
$list_of_nums[]=$temp;
}

return $list_of_nums;
}

$list_of_nums = random_nums();

foreach($list_of_nums as $num) {
echo &quot;$num<BR>&quot;;
}

?>
 
sorry, I need to play with things more.

I tried to change it in the function, which was not the right thing to do :)


But, when I changed
$list_of_nums = random_nums();
to
$list_of_nums = random_nums($x,$min,$max);
it worked.


 
Probably because $images is less than 10, in which case that function will be screwy... I thought i had posted a version with error checking too, but maybe not.

Anyway, that function returns a list of random numbers ($x of them to be exact)... and you want two, so I'd say put the original function as it was and then call it as

$list_of_nums=random_nums(2,0,$images);

foreach $list_of_nums as $num) {
echo &quot;$num<BR>&quot;;
}

 
Why not:

Count the number of files in the directory (call it n).
Pick a random number between 1 and n. Call it m
do
{
pick another random number between 1 and n. call it o.
}
while m != 0

Count through the files in the directory, outputting file number m and o.

Want the best answers? Ask the best questions: TANSTAAFL!
 
thanks skiflyer, I did do

$list_of_nums=random_nums(2,0,$images);

foreach $list_of_nums as $num) {
echo &quot;$num<BR>&quot;;
}

before I read your post, and it worked :) Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top