Posted By

mawady on 07/05/11


Tagged

random


Versions (?)

Un-repeated Random Variables


 / Published in: PHP
 

Generating Array of Un-repeated Random Variables In User-Defined Bounds

  1. <?php
  2. function GetUnrepeatedRandomVariables($minIndex,$maxIndex)
  3. {
  4. $res = array();
  5.  
  6. while(1)
  7. {
  8. if(count($res) == ($maxIndex-$minIndex+1))
  9. break;
  10. $rand = rand($minIndex, $maxIndex);
  11. if(!in_array($rand, $res))
  12. array_push ($res, $rand);
  13. }
  14. return $res;
  15. }
  16.  
  17. $arr = GetUnrepeatedRandomVariables(0, 10);
  18. print (count($arr) ."<br />");
  19. foreach ($arr as $val)
  20. print($val ."<br />");
  21. ?>

Report this snippet  

You need to login to post a comment.