Generate array of different/unique random numbers in PHP
Sometime it is necessary to generate a list of different random numbers. A possible usage scenario therefore could be a raffle where the winners should be choosen by chance, but every single user shouldn’t get more than one prize.
So in the following snippet, I’ll show you how to generate a list of different randoms in PHP.
<?php
//Create an array of numbers from which the randoms
//should be choosen. (For raffle: the list of user id’s)
$array = range($minimum, $maximum);
//Initialize the random generator
srand ((double)microtime()*1000000);
//A for-loop which selects every run a different random number
for($x = 0; $x < $numberOfRandoms; $x++)
{
     //Generate a random number
     $i = rand(1, count($array))-1;
     //Take the random number as index for the array
     $result[] = $array[$i];
     //The chosen number will be removed from the array
     //so it […]
																			