Jump to content

array_rand() not really randomizing.


play_

Recommended Posts

Although I found a way around it, I want to know, logically, why the behavior is so.

 


// words.txt is a text file with dictionary words on each line
$lines = file('words.txt');

// The goal is to pick two random words, 10 times.
for($i = 0; $i < 10; $i++) {
#shuffle($lines); <--- un-commenting this is the fix i found.
$words = array_rand($lines, 2);
echo "Words: " . $lines[$words[0]] . ", " . $lines[$words[1]] . "<br>";
}

 

If i don't have shuffle() in there, I get two different set of words, not 10. ie (result from code above)

 

Words: massacre , spear

Words: frontiersman , kumquat

Words: massacre , spear

Words: frontiersman , kumquat

Words: massacre , spear

Words: frontiersman , kumquat

Words: massacre , spear

Words: frontiersman , kumquat

Words: massacre , spear

Words: frontiersman , kumquat

 

 

Why is this?

Link to comment
Share on other sites

Weird, works fine for me. I used a txt file with a number on each line from 1 to 100. These were my results:

Words: 18 , 62
Words: 58 , 92
Words: 13 , 80
Words: 65 , 95
Words: 80 , 82
Words: 4 , 61
Words: 1 , 79
Words: 3 , 43
Words: 69 , 78
Words: 13 , 82 

 

Note that the ORDER of the results is not random in that for each pair the lower value is always first. So, if it matters if the order of the pair is random you would want to be using shuffle() anyways

Link to comment
Share on other sites

The first thing is that if you shuffle() the original array there is no point in calling array_rand().  As mjdamato showed... one thing I noticed is that they changed the way the function works recently.  The old array of keys used to be shuffled but now you get them in order.  I don't know how many elements you have in your words.txt, but as it is now you will always get the keys in order.  You could shuffle() the keys, but you might just as well shuffle the $lines array.  At any rate:

 

This code:


for ($i=0; $i         $lines = array('apple', 'pear', 'banana', 'kiwi', 'raspberry', 'pineapple');
        $keys = array_rand($lines, 2);
        echo "Pass $i\n";
        var_dump($keys);
}

 

Produces:

[david@benji ~]$ php -f arrayrand.php 
Pass 0
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(5)
}
Pass 1
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
Pass 2
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(4)
}
Pass 3
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(1)
}
Pass 4
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(4)
}
Pass 5
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(5)
}
Pass 6
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(3)
}
Pass 7
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(1)
}
Pass 8
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(2)
}
Pass 9
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(1)
}

 

See the problem?

Link to comment
Share on other sites

Weird, works fine for me. I used a txt file with a number on each line from 1 to 100. These were my results:

Words: 18 , 62
Words: 58 , 92
Words: 13 , 80
Words: 65 , 95
Words: 80 , 82
Words: 4 , 61
Words: 1 , 79
Words: 3 , 43
Words: 69 , 78
Words: 13 , 82 

 

Note that the ORDER of the results is not random in that for each pair the lower value is always first. So, if it matters if the order of the pair is random you would want to be using shuffle() anyways

 

Interesting. If i replace the contents of words.txt with a number on each line, i get

 

Words: 2 , 9

Words: 8 , 9

Words: 5 , 10

Words: 2 , 7

Words: 9 , 10

Words: 4 , 10

Words: 6 , 8

Words: 8 , 10

Words: 2 , 5

Words: 1 , 9

 

words.txt is just list of 5000+ words.

 

 

 

 

 

@Gizmola - no. i don't see the problem there. Your results seem random.

Link to comment
Share on other sites

I pointed out the same thing that mjdamato did.

 

Although you get an array with 2 keys in it... the keys are ordered smallest to largest.  Notice you never get an array of:

 

9, 3

 

It will always be 3, 9.

 

5k words is large enough of an array that I would not want to be shuffling that every time, but that's just a guess.  You would have to profile the different approaches:

 

-shuffle() whole array

-shuffle(array_keys())

-$max = count($lines)-1; $first = $lines[rand(0, $max)]; $lines[rand(0, $max)]; 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.