darthbutternutz Posted April 28, 2009 Share Posted April 28, 2009 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 Link to comment https://forums.phpfreaks.com/topic/155960-newb-question-regarding-math-random-variables-and-if-statements/ Share on other sites More sharing options...
Prismatic Posted April 28, 2009 Share Posted April 28, 2009 <?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) : ""); ?> Link to comment https://forums.phpfreaks.com/topic/155960-newb-question-regarding-math-random-variables-and-if-statements/#findComment-820971 Share on other sites More sharing options...
darthbutternutz Posted April 28, 2009 Author Share Posted April 28, 2009 <?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? Link to comment https://forums.phpfreaks.com/topic/155960-newb-question-regarding-math-random-variables-and-if-statements/#findComment-821379 Share on other sites More sharing options...
darthbutternutz Posted April 28, 2009 Author Share Posted April 28, 2009 <?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.... Link to comment https://forums.phpfreaks.com/topic/155960-newb-question-regarding-math-random-variables-and-if-statements/#findComment-821385 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.