randomNum duplicating some results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malloy
Forum Newbie
Posts: 15
Joined: Fri Jun 01, 2007 2:02 am
Location: Melbourne, Australia

randomNum duplicating some results

Post by malloy »

Hi Everyone,


I have the following code to display random pictures.

Code: Select all

<?php
$ban1 = "apple.jpg";
$url1 = "http://www.apple.com";
$ban2 = "orange.jpg";
$url2 = "http://www.orange.com";
$ban3 = "banana";
$url3 = "http://www.banana.com;

$randomNum = rand (1,3);

$image = ${'ban'.$randomNum};

$url= ${'url'.$randomNum};

Print "<a href=".$url." mce_href=".$url." target=\"_blank\" ><img src=".$image." border=0 class=\"thumbs\" width=\"180\" height=\"135\"></a>";
?>
I am then using a simple include statement within my html to call the php.

Code: Select all

td><?php include("image_thumbs.php"); ?></td>
I have been trying to alter my code so that an image cannot be repeated twice. In my full code I have 200 images and each page has 30 include statements. It just some times a picture is repeated twice or even three times.

Does any one have any suggestions how i could get around this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How often do you not want them to repeat? If it is every 200 times then just cycle through the list. You could save the current position in the session. Maybe you could start at a random image if the value in the session has not yet been set.
(#10850)
malloy
Forum Newbie
Posts: 15
Joined: Fri Jun 01, 2007 2:02 am
Location: Melbourne, Australia

Post by malloy »

Hi arborint,

The way it works at the moment is i have a folder with 200 images. Each time the webpage is loaded the script gathers and displays 30 of them randomly.
Each time the page is refreshed a new random lot of 30 images appear.

The problem is i'm getting duplicate images appear.

Ideally when the page is refreshed i would like 30 individual images to appear with no duplicates. Thats what i cant figure out what to do.

Any help would be greatly appreciated.

Kind regards,

David
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

What I would do is create a list of all images in the directory using scandir() then pick 30 random numbers, each time comparing them to a list of previous numbers, where each number is an index in the list.

Code: Select all

$imgs=scandir("path/to/imgs/",1);//collect the imgs
$imgs=array_diff($imgs,array(".","..");//get rid of the dot filenames
$used=array();//the used random numbers
for($i=0;$i<30;$i++){//pick a random # 30 times
  $r=rand(0,200); //pick the #
  if(!in_array($r,$used)) //if it is not in the used #s:
    $used[]=$r; //add it to the list
}

for($j=0;$j<count($used);$j++){ //loop through the 30 random numbers
  $img=$imgs[$used[$j]];
  // print the img tags
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I would put all the image names into an array, use shuffle() to randomize it, and then use array_slice() to get the first 30 values.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I would use scandir() to collect all the images, pull the lot into an array, then use array_rand() to pull out 30 random entries.

edit: lol, beaten by astions!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

As for the original question, the birthday paradox would ensure that probability of repeated samples when picking 30 independent samples off a pool of 200 would be quite big.
Post Reply