Jump to content

Please help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Steviepoofs

Steviepoofs

    Newbie

  • Members
  • Pip
  • 4 posts
Will someone please explain why the below code does not work and how I can fix it?



if ($_REQUEST['ET']){

	$Threshold = $_REQUEST['txtTH'];$i=0;$x=0;$hit=0;$DicePool=$_REQUEST['txtDice'];

	

	while($hit < $Threshold){

		while($i < $DicePool){

			$Roll = rand(1,6);

			if ($Roll >= 5) $hit++;

			$i++;

		}

		$x++;

	}


	

	$Output = '<b>'.$x. '</b> Iterations for <b>'.$hit.'</b> hits <br>Threshold='.$Threshold.'<br>Dice Pool='.$DicePool;

}



Edited by Steviepoofs, 07 October 2010 - 12:18 PM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
In what sense is it not working?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Steviepoofs

Steviepoofs

    Newbie

  • Members
  • Pip
  • 4 posts
It seems to be caught in an infinite loop, as in when data is passed from the form the server returns no data and the page is infinitely loading.

#4
Steviepoofs

Steviepoofs

    Newbie

  • Members
  • Pip
  • 4 posts
input form can be found at The Roller...

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I didn't get any infinite loops. What input are you using to duplicate it?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Steviepoofs

Steviepoofs

    Newbie

  • Members
  • Pip
  • 4 posts
I actually came up with a different method:
function Roll($theDice) {
		while($i < $theDice){
			$Roll = rand(1,6);
			if ($Roll >= 5) $_SESSION['hit']++;
			$i++;
		}
}

if ($_REQUEST['ET']){
	$threshold = $_REQUEST['txtTH'];$i=0;$x=0;$hit=0;$Dice=$_REQUEST['txtDice'];

	while ($_SESSION['hit'] < $threshold){
		Roll($Dice);
		$x++;
	}


Using the session var. I was able to get it to work. I'm guessing that my orginal code was not working because of the var $hits was not being passed between the two loops?

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
while($hit < $Threshold){
    while($i < $DicePool){
        $Roll = rand(1,6);
        if ($Roll >= 5) $hit++;
        $i++;
    }
    $x++;
} 

$i has a high chance of becoming larger than $DicePool and break out without $hit becoming larger than $Threshold, thus the outter loop will be infinite as it cannot be satisfied.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.