play_ Posted May 26, 2011 Share Posted May 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/237490-array_rand-not-really-randomizing/ Share on other sites More sharing options...
Psycho Posted May 26, 2011 Share Posted May 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237490-array_rand-not-really-randomizing/#findComment-1220386 Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/237490-array_rand-not-really-randomizing/#findComment-1220388 Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 One other thing is that if these arrays are going to get large at some point, it would probably end up being much faster for you to just generate 2 random numbers inside your loop and use those as keys. Quote Link to comment https://forums.phpfreaks.com/topic/237490-array_rand-not-really-randomizing/#findComment-1220389 Share on other sites More sharing options...
play_ Posted May 26, 2011 Author Share Posted May 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237490-array_rand-not-really-randomizing/#findComment-1220393 Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 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)]; Quote Link to comment https://forums.phpfreaks.com/topic/237490-array_rand-not-really-randomizing/#findComment-1220403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.