Jump to content

Recommended Posts

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.

 

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. 

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).

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.