This is no good nonsense;
The first is to use the Mt_rand () function to generate a specified number of random numbers;
Then use the Array_unique () function to go heavy;
Because go heavy, so get the number is not enough to specify the number of;
So, the core is to use the while loop, until the number of the specified number is obtained;
The basic can be the end here;
For the perfect person, you can also use a sort ();
The purpose is not to be used for sorting; The key is to format the resulting array key;
To speak in code, as follows;
/** * Generate non-repeating random number * @param int $start need to generate the number start range * @param int $end End range * @param int $length The number of random numbers to generate * the random number generated by the @return array */function get_rand_number ($start =1, $end =10, $length =4) { $connt =0; $temp =array (); while ($connt < $length) { $temp []=mt_rand ($start, $end); $data =array_unique ($temp); $connt =count ($data); } Sort ($data); return $data;}
Of course, the emphasis is not on this function; The emphasis is on the idea of this function; it can solve many similar problems;