最新消息: 新版网站上线了!!!

php生成随机数

  1. //第一方法 
  2.  
  3. $numbers = range(1, 20); 
  4. shuffle($numbers); 
  5.  
  6. //第二种方法 
  7.  
  8. function UniqueRandomNumbersWithinRange($min, $max, $quantity) { 
  9.     $numbers = range($min, $max); 
  10.     shuffle($numbers); 
  11.     return array_slice($numbers, 0, $quantity); 
  12. } 
  13.  
  14. Example: 
  15.  
  16. <?php 
  17. print_r( UniqueRandomNumbersWithinRange(0,25,5) ); 
  18. ?> 
  19. /* 
  20. Result: 
  21.  
  22.  Array 
  23. ( 
  24.     [0] => 14 
  25.     [1] => 16 
  26.     [2] => 17 
  27.     [3] => 20 
  28.     [4] => 1 
  29. )*/ 

 

转载请注明:谷谷点程序 » php生成随机数