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

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. Example: 
  14.  
  15. <?php 
  16. print_r( UniqueRandomNumbersWithinRange(0,25,5) ); 
  17. ?> 
  18. /* 
  19. Result: 
  20.  
  21.  Array 
  22. ( 
  23.     [0] => 14 
  24.     [1] => 16 
  25.     [2] => 17 
  26.     [3] => 20 
  27.     [4] => 1 
  28. )*/ 

 

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