Jump to content

Newb Question regarding Math, Random Variables, and If Statements


darthbutternutz

Recommended Posts

First time poster, long time lurker...

 

I'm trying to figure out how to put a certain set of math rules into code and I can't seem to get it to work. My goal is to start with a random number between 3 and 18. If the sum of that random is 16 or greater, then add a random number between 1 and 6 to the sum of that value. If the sum of the second random is equal to 6, then add another random number between 1 and 6.

 

This is what I have so far

<php?

$roll1=rand(3,18);
$roll2=rand(1,6);
$roll3=rand(1,6);


if ($roll1<=16)
{
	$goodroll=$roll1+$roll2;
}
else
{
	$roll1=$finalroll;
}

if ($roll2=6)
        {
                $superroll=$goodroll+$roll3;
        }
else
{
	$goodrole=$finalroll;
}

echo $finalroll;

?>

 

I'm sure I've messed up with the code. I'm not sure if I declared my variables correctly or even if my If Statements are written correctly.

 

Any help would be awesome :)

<?php
/** Initial sums */
$sum_1 = rand(3, 18);
$sum_2 = rand(1, 6);
$sum_3 = $sum_2 + $sum_1;

echo ($sum_1 >= 16 ? ($sum_2 = 6 ? $sum_3 + rand(1, 6) : $sum_3) : "");
?>

 

Wow thanks for the response. For some reason though, the echo isn't displaying a number. But if I.....

echo $sum_1;
echo $sum_2;
echo $sum_3;

then I get some numbers. But every once in a while, I get extremely large numbers like 2521 and 2321. Whats going on there?

 

<?php
/** Initial sums */
$sum_1 = rand(3, 18);
$sum_2 = rand(1, 6);
$sum_3 = $sum_2 + $sum_1;

echo ($sum_1 >= 16 ? ($sum_2 = 6 ? $sum_3 + rand(1, 6) : $sum_3) : "");
?>

 

Wow thanks for the response. For some reason though, the echo isn't displaying a number. But if I.....

echo $sum_1;
echo $sum_2;
echo $sum_3;

then I get some numbers. But every once in a while, I get extremely large numbers like 2521 and 2321. Whats going on there?

 

The large numbers seem to be coming from the first variable $sum_1....

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.