This was extremely helpful! Thank you! I looked for this for a long time around the search engines and PHP sites and couldn't find this info. Should have started at DP. Lesson learned.
The way I'm going to do it is: <?php $numbers = array(0,1,2,3,4,5,6,7,8,9); $selected = array_rand($numbers,4); ?> This will actually return the keys that point to the numbers, so you could put any items you want into the array (in the case of the array I've supplied, these are the same as the contents). I generally use this for rotating banners. I was quite surprised to find that rand() doesn't allow picking several numbers at once to avoid duplication.
You could generate numbers individually, store them to an array, check that the generated number is not already in the array, and if it is reject the number and generate a new one.
thanks you for that! Could you help me a bit more on this im a noob at php im going through tiutorials at the moment but not confident enough to know how to do this!Thanks again
OK. There are several ways to generate more than one unique random number in a range. The first is to store the range as an array and use array_rand to pick them all at the same time, as I explained in my earlier post: <?php $numbers = array(0=>0,1,2,3,4,5,6,7,8,9); $selected = array_rand($numbers,4); ?> Notes re above method: 1. This is a cheat, really, because array_rand picks up keys, not the contents, and you will notice I've added 0=> inside the assignment brackets, because if your random number range doesn't start at 0 (and assuming your number set is whole numbers in a range incrementing by 1), you're going to have to assign the first one a key so that it works. This key should be the same as the first number in the range, so if your range is from 3 to 7, then the brackets would be (3=>3,4,5,6,7), and so on. I hope this is clear. 2. The new variable $selected is an array containing the 4 numbers selected from the original array. To refer to each row in the array use $selected[0] through $selected[3]. If you want to put them in an echo statement use {} round them: <?php echo "The numbers are {$selected[0]}, {$selected[1]}, {$selected[2]} and {$selected[3]}"; ?> The second way, which has none of the previous one's problems, is to store all the numbers in an array, shuffle them, and then pop one value at a time off the array (which removes it from the array, so it can't be used again): <?php //set up the array sometime earlier and shuffle it $numbers = array(0,1,2,3,4,5,6,7,8,9); shuffle($numbers); //use the following command whenever you want to extract a single number from the array $selected = array_pop($numbers); ?> It's safest to use {} if you want to print it in an echo statement, as before: echo "The number is {$selected}"; ?> The third way is to store numbers as you generate them to an array, and to check the number isn't already present in the array whenever you generate a new one. However, having thought about this, I think you would be better using one of the solutions above, as they will be quicker.
class UniqueRandomSet{ static $questions=array(); function __construct($questionsRequired,$questionsAvailable){ for($n=0;$n<$questionsRequired;$n++){ //print $n."<br/>"; $x=rand(0,$questionsAvailable-1); if (in_array($x,self::$questions)) { $n--; }else{ self::$questions[]=$x; } } //return $this->questions; } } //------------ Test it ------- $r=new UniqueRandomSet(5,33); for($n=0;$n<5;$n++){ print UniqueRandomSet::$questions[$n]."<br/>"; }