Advertisement
rilo

PHP shuffle() bug

Oct 16th, 2014 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1. <?php
  2. function _fork() {
  3.     $pid = pcntl_fork();
  4.     if ($pid) {
  5.         return $pid;
  6.     } else {
  7.         $txt = '';
  8.         for ($j = 0; $j < 5; $j++) {
  9.             $rnd = rand(0, 9);
  10.             $txt .= "$rnd ";
  11.         }
  12.         echo "$txt\n";
  13.         exit;
  14.     }
  15. }
  16.  
  17. $z = array();
  18. shuffle($z);
  19. for ($i = 0; $i < 5; $i++) {
  20.     _fork();
  21. }
  22.  
  23. usleep(10000);
  24. pcntl_wait($status);
  25. echo "\n";
  26.  
  27. $z[] = "Hello";
  28. shuffle($z);
  29. for ($i = 0; $i < 5; $i++) {
  30.     _fork();
  31. }
  32.  
  33. usleep(10000);
  34. pcntl_wait($status);
  35. echo "\n";
  36.  
  37. $z[] = "World";
  38. shuffle($z);
  39. for ($i = 0; $i < 5; $i++) {
  40.     _fork();
  41. }
  42.  
  43.  
  44. /** Sample output:
  45. 3 0 7 0 9 // Wrong
  46. 9 4 2 5 1
  47. 2 5 6 1 0
  48. 8 3 5 6 5
  49. 1 6 7 5 5
  50.  
  51. 6 0 2 6 3 // Wrong
  52. 8 5 8 2 0
  53. 4 1 3 0 4
  54. 2 1 5 1 4
  55. 6 8 2 3 2
  56.  
  57. 1 3 5 0 6 // Right
  58. 1 3 5 0 6
  59. 1 3 5 0 6
  60. 1 3 5 0 6
  61. 1 3 5 0 6
  62. **/
  63. ?>
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement