Pwned9080 Posted May 21, 2010 Share Posted May 21, 2010 Hello, I'm having a problem, I don't think the problem is in the code, but I was wondering, if I repeat some code a bunch of times is it possible that PHP is lazy and just repeats previous results? Like so(Not the actual code), <?php $var_a = 1; $var_b = 50; while($var_a <= $var_b) { $file1 = file("1.txt"); $file2 = file("2.txt"); $file3 = file("3.txt"); $one = $file1[array_rand($file1)]; $two = $file2[array_rand($file2)]; $three = $file3[array_rand($file3)]; echo $one . ", " . $two.", ".$three; $var_a++; } ?> The actual code is much bigger, uses a lot more variables(and files), and repeats results 1-12/13 over and over until it hits $var_b. If this has been asked before I'm sorry, I'm just not sure what to search for. Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/ Share on other sites More sharing options...
DavidAM Posted May 21, 2010 Share Posted May 21, 2010 It is unlikely that PHP is being "lazy". It is more likely that the files being loaded are short enough that randomizing them returns the same value often enough to be noticed. I would suggest an alternate approach, for that reason as well as efficiency. You are reading the same file into an array over and over again. Why not pull that outside the loop: $file1 = file("1.txt"); $file2 = file("2.txt"); $file3 = file("3.txt"); shuffle($file1); shuffle($file2); shuffle($file3); $var_a = 1; $var_b = 50; while($var_a <= $var_b) { $one = array_pop($file1); $two = array_pop($file2); $three = array_pop($file3); echo $one . ", " . $two.", ".$three; $var_a++; } Of course if there are less than 50 entries in any of the files, that will produce an error. Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1061807 Share on other sites More sharing options...
Pwned9080 Posted May 21, 2010 Author Share Posted May 21, 2010 I'm using a function to grab the lines, function rand_line($file) { $file0 = file($file); $result = trim($file0[array_rand($file0)]); return $result; } But it's grabbing the same lines along side the same lines, a, b, c a, a, b ... after twelve or so unique results it starts over, in the same order and everything. I also seriously doubt that it's the lack of lines in the text files, there are 18+ text files it's grabbing data from, with the smallest have 5 lines, the largest having 745 lines. It's not that $one is returning the same value a couple times, it's that they line up(For lack of a better way of saying what I mean. Also my PHP version is 5.2.9 Apache 2.2.15 I tried increasing the memory scripts were allowed to use from 128M to 512M with no change, and restarting apache increased the unique results by about two. EDIT: I want each one to be able to repeat, just not as a group(getting aaba over and over again). Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1061823 Share on other sites More sharing options...
Daniel0 Posted May 21, 2010 Share Posted May 21, 2010 Could you attach {1,2,3}.txt? Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1061824 Share on other sites More sharing options...
Pwned9080 Posted May 21, 2010 Author Share Posted May 21, 2010 Do you want all of them(18+), and I'm not sure they'd be any good without the whole code. Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1061825 Share on other sites More sharing options...
Pwned9080 Posted May 22, 2010 Author Share Posted May 22, 2010 I uploaded my script and text files to x10hosting.com, and got 500 unique results, could it be a problem with my computer or install? Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1061829 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 It would sound like your PRNG is messed up somehow. Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1061896 Share on other sites More sharing options...
Pwned9080 Posted May 23, 2010 Author Share Posted May 23, 2010 Any idea of how to fix it? Reinstall? Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1062122 Share on other sites More sharing options...
Pwned9080 Posted May 25, 2010 Author Share Posted May 25, 2010 I found the reason why it was repeating, anyone else with this same problem: http://www.boallen.com/random-numbers.html Thanks for those that helped. Quote Link to comment https://forums.phpfreaks.com/topic/202539-random-generator-reprinting-same-results/#findComment-1063313 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.