Page 1 of 1

randomNum duplicating some results

Posted: Tue Dec 11, 2007 6:46 pm
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?

Posted: Tue Dec 11, 2007 6:58 pm
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.

Posted: Tue Dec 11, 2007 8:01 pm
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

Posted: Tue Dec 11, 2007 8:14 pm
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
}

Posted: Tue Dec 11, 2007 8:25 pm
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.

Posted: Tue Dec 11, 2007 8:26 pm
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!

Posted: Wed Dec 12, 2007 1:44 am
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.