Posted By

kurokikaze on 01/28/09


Tagged

random pairs


Versions (?)

Random pairing


 / Published in: PHP
 

URL: kurokikaze.tumblr.com

Returns specified number of random pairs.

  1. function random_pairing($pairs) {
  2. $out = array();
  3.  
  4. // Fail if number of items is odd
  5. $first = array();
  6. $second = array();
  7.  
  8. for($i = 0; $i < $pairs * 2; $i++){
  9.  
  10. $nums[] = $i;
  11.  
  12. }
  13.  
  14. shuffle($nums);
  15.  
  16. $first = array_slice($nums, 0, $pairs);
  17. $second = array_slice($nums, $pairs, $pairs);
  18.  
  19. shuffle($second);
  20.  
  21. return array_combine($first, $second);
  22. }

Report this snippet  

You need to login to post a comment.