I've been tearing my hair out over this one, I am missing a fundimental thing.

I am trying to generate 3 random numbers from an arbritrary pool, say from 1 to 3, so I should get 1, 3, 2 or a permientation of that. But I simply can't get my head around how to stop it generating the same number, this is what I have:

for($thumbs=1; $thumbs<=3; $thumbs++)
{
$randphotos[$thumbs] = rand(1,$nophotos);
echo "$randphotos[$thumbs]<BR>";
}

Those are the building blocks, but beyond that I get lost immediately!

    In this case you need to use [man]array_rand[/man]. Here is modified example form the help:

    srand ((float) microtime() * 10000000);
    $input = array (1,2,3);
    $rand_keys = array_rand ($input, 3);
    print $input[$rand_keys[0]]."\n";
    print $input[$rand_keys[1]]."\n";
    print $input[$rand_keys[2]]."\n";
    

      Can than not select the same value again?

        No. It doesn't provide duplicates. Only uniques.

          ok, it looks promising, however, I want to change the range of numbers dynamically. 1 to 3 was an example, how would I say go from 1 to $dynamicnumber without manually counting it into an array, is there a count function?

            yes, it's called count() 😉
            you can modify the example IceD gave like this

            srand ((float) microtime() * 10000000);
            $input = array (1,2,3,4,5,6);
            $rand_keys = array_rand ($input, count($input);
            for ($i=0; $i<count($input); $i++) {
                print $input[$rand_keys[$i]]."\n";
            }
            

              You can use [man]range[/man]($low, $high[, $step]) function for generating ranges and [man]count[/man] for counting number of items. So here is example, fixed once again:

              srand ((float) microtime() * 10000000);
              $input = range(1,10);
              $rand_keys = array_rand($input, count($input));
              for ($i=0; $i<count($input); $i++)
              {
                print $input[$rand_keys[$i]]."\n";
              }
              

              BTW. You can use [man]shuffle[/man] function as well in this case example will be:

              srand ((float) microtime() * 10000000); 
              $input = range(1,10);
              shuffle($input);
              for ($i=0; $i<count($input); $i++)
              {
                print $input[$i]."\n";
              }
              
                 	 for($thumbs=1; $thumbs<=$nophotos; $thumbs++) 
                	 		{
                			srand ((float) microtime() * 10000000);
                			$randphotos[$thumbs] = $thumbs;
                
                		$rand_keys = array_rand($randphotos, $thumbs);
                
                		}
                		print $randphotos[$rand_keys[0]]."\n";
                		print $randphotos[$rand_keys[1]]."\n";
                		print $randphotos[$rand_keys[2]]."\n";		

                This is what I have now, and its doing the trick nicely, $nophotos is taken from a database....

                  Your code isn't "nice". Much better is:

                  $randphotos = range(1, $nophotos);
                  srand ((float) microtime() * 10000000);
                  shuffle($randphotos);
                  for ($i = 1; $i < $notodisplay; $i++)
                    display_photo($randphotos[$i]);
                  

                    Right, I think I've got the hang of that, thanks for the help,

                      Yeah. PHP community is one of da best. Just ask and you will get the answer 😉

                        Write a Reply...