Jump to content

Recommended Posts

I need a bit of help.

I'm at the end of my rope for the night.

I'm a total noob when it comes to programming, and i'm trying to make a bit of a complex randomizer generate 6 unique numbers for me.

 

For some reason, this simple randomize code throws my html browser into an infinite loop every now and then. It's weird, because most of the time it works, but every now and then the while statements are causing it to hit an infinite loop, (which is obviously no code).

 

Can someone help me find where the problem is stemming from?

 

Code:

<?php

function NetworkInfo ($textfieldUsername) {

$myFile = "../../USER_INFO/" . $textfieldUsername . "-Network.txt";

$fh = fopen($myFile, 'r');

$Gate = "__";

while (strcmp(trim($Gate),trim($ZeroHolder)) <> 0) {

$counter = $counter + 1;

$Gate = fgets($fh);

}

$TotalNetworkNums = $counter - 1;

if ($TotalNetworkNums > 0) {

$FirstNetworkNum = rand (1, $TotalNetworkNums);

}

if ($TotalNetworkNums > 1) {

$SecondNetworkNum = rand (1, $TotalNetworkNums);

do {

$SecondNetworkNum = rand (1, $TotalNetworkNums);

} while ($SecondNetworkNum==$FirstNetworkNum);

}

if ($TotalNetworkNums > 2) {

$ThirdNetworkNum = rand (1, $TotalNetworkNums);

do {

$ThirdNetworkNum = rand (1, $TotalNetworkNums);

} while (($FirstNetworkNum==$ThirdNetworkNum) || ($SecondNetworkNum==$ThirdNetworkNum));

}

if ($TotalNetworkNums > 3) {

$FourthNetworkNum = rand (1, $TotalNetworkNums);

do {

$FourthNetworkNum = rand (1, $TotalNetworkNums);

} while (($FirstNetworkNum==$FourthNetworkNum) || ($SecondNetworkNum==$FourthNetworkNum) || ($ThirdNetworkNum==$FourthNetworkNum));

}

if ($TotalNetworkNums > 4) {

$FifthNetworkNum = rand (1, $TotalNetworkNums);

do {

$FifthNetworkNum = rand (1, $TotalNetworkNums);

} while (($FirstNetworkNum==$FifthNetworkNum) || ($SecondNetworkNum==$FifthNetworkNum) || ($ThirdNetworkNum==$FifthNetworkNum) || ($FourthNetworkNum==$FifthNetworkNum));

}

if ($TotalNetworkNums > 5) {

$SixthNetworkNum = rand (1, $TotalNetworkNums);

do {

$FifthNetworkNum = rand (1, $TotalNetworkNums);

} while (($FirstNetworkNum==$SixthNetworkNum) || ($SecondNetworkNum==$SixthNetworkNum) || ($ThirdNetworkNum==$SixthNetworkNum) || ($FourthNetworkNum==$SixthNetworkNum) || ($FifthNetworkNum==$SixthNetworkNum));

}

echo "" . $FirstNetworkNum . " " . $SecondNetworkNum . " " . $ThirdNetworkNum . " " . $FourthNetworkNum . " " . $FifthNetworkNum . " " . $SixthNetworkNum . " ";

fclose($fh);

}

NetworkInfo ($_REQUEST['textfieldUsername']);

Link to comment
https://forums.phpfreaks.com/topic/209937-help-with-a-simple-randomizer-code/
Share on other sites

Thanks for the reply,

but I already considered that.

It doesn't appear to be the problem though.

 

I removed all the other while statements from the code, but left that one in and tested it.

It comes up fine every time, with no infinite loop problems.

Also, if that was the problem, wouldn't it create an infinite loop EVERY time?

 

I believe it's in the || (or) statements, which is why it's only occurring from time to time.

Does this not seem like a logical train of that?

That and the code works with the ZeroHolder while statement. heh

(Using an undefined variable always results in the result of an absolute zero. This is handy when trimming for string comparisons).

 

Again, thanks for the reply.

But keep 'em coming,

this one still ain't solved. :)

 

Update:

 

Figured it out.

Man, it's been a long day.

 

It was in my last "if"/"while" statement:

I accidentally had the do/while statement restriping FifthNetworkNum with data instead of SixthNetworkNum when SixthNetworkNum was == to the other "$NetworkNums".

 

So yea, when the SixthNetworkNum would be equal to the others, it wasn't being rearranged. It would just throw the computer into an infinite loop.

 

This is also why it was only occuring occasionally.

 

Here's the faulty code I'm talking about:

"

                          if ($TotalNetworkNums > 5) {

                              $SixthNetworkNum = rand (1, $TotalNetworkNums);

                              do {

 

                                // 'Here was the problem, from here.'

 

                                $FifthNetworkNum = rand (1, $TotalNetworkNums); 

 

                                // 'To here.'

 

                              } while (($FirstNetworkNum==$SixthNetworkNum) || ($SecondNetworkNum==$SixthNetworkNum) || ($ThirdNetworkNum==$SixthNetworkNum) || ($FourthNetworkNum==$SixthNetworkNum) || ($FifthNetworkNum==$SixthNetworkNum));

                              }     

                          echo "" . $FirstNetworkNum . " " . $SecondNetworkNum . " " . $ThirdNetworkNum . " " . $FourthNetworkNum . " " . $FifthNetworkNum . " " . $SixthNetworkNum . " ";

                          fclose($fh);

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.