3
function nonrecgen($min, $max, $amount) {
for($i=0;$i<$amount;$i++) {

$NrArray[$i] = rand($min,$max);
echo $NrArray[$i];
do  {
    for($j=0;$j<=$i;$j++) {
      if ($NrArray[$j] == $NrArray[$i]) {
      $NrArray[$i] = rand($min,$max);   }     
                          }
       $Reccuring = false;
   if ($i > 0) {
    for($k=0;$k<=$i;$k++) {
      if ($NrArray[$k] == $NrArray[$i]) {
       $Reccuring = true;               }
                          }
               }
    }
while ($Reccuring = true);
                          }
Return $NrArray;                                        
                                        }

$Test = nonrecgen(0,1,2);
print_r($Test);

I wanted to look into how to generate an array of nonreccuring numbers and while this is certainly not the most efficient way I believe, I can't seem to figure out why it loops endlessly on the first iteration. I tried logical analysis over and over, but there has to be something I'm missing.

3
  • 11
    Who needs sensible indentation, after all.
    – user895378
    Commented Aug 21, 2012 at 19:43
  • 1
    FYI, this is called an infinite loop. Also, @andrewsi's solution is correct.
    – Matt
    Commented Aug 21, 2012 at 19:44
  • 2
    @rdlowrey indeed ... or contrivances like a spelling checker
    – supervacuo
    Commented Aug 21, 2012 at 19:51

3 Answers 3

29
do {
...
} while ($Reccuring = true);

Because your while statement sets $Reccuring to true, instead of evaluating it.

Try:

do {
...
} while ($Reccuring === true);
2
  • ===? Would that be necessary for true. I have no clue about PHP, but the other answers only have ==. If it is advisable/necessary it is probably worth elaborating on. Commented Oct 27, 2014 at 16:09
  • 4
    @BillWoodger - with this code, it won't make a difference, as he's explicitly using just true and false; I tend to use === as a default in my own code, just because it's a stricter check.
    – andrewsi
    Commented Oct 27, 2014 at 16:17
6

Other than the = to == you were also resetting the $Recurring in the wrong place:

<?
function nonrecgen($min, $max, $amount) 
{
    for($i=0;$i<$amount;$i++) 
    {
        $NrArray[$i] = rand($min,$max);
        do  
        {
            for($j=0;$j<=$i;$j++) 
            {
                if ($NrArray[$j] == $NrArray[$i]) 
                {
                    $NrArray[$i] = rand($min,$max);  
                }     
            }
            if ($i > 0) 
            {
                for($k=0;$k<=$i;$k++) 
                {
                    if ($NrArray[$k] == $NrArray[$i]) 
                    {
                        $Reccuring = true;               
                    }
                }
            }
            $Reccuring = false;
        }
        while ($Reccuring == true);
    }
    return $NrArray;                                        
}
$Test = nonrecgen(0,2,5);
echo "<pre>";
print_r($Test);
?>
6

You're currently assigning a value rather than checking (which will always be true).

Change it to: while ($Reccuring == true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.