Page 1 of 1

Random Percentages

Posted: Thu Oct 09, 2014 7:17 pm
by Epiales
Okay, I'm trying to work out an attack scenario, and think I want to try using percentages, but have no idea how to make that happen.

I know I can use the rand(1,5) or whatever to choose between 1 and 5, but how would I create it so that it would randomly be between 1 and 5 percent instead?

Re: Random Percentages

Posted: Thu Oct 09, 2014 7:54 pm
by Sharlenwar
Percentage is usually like 0.01 to 0.05 for 1%-5%. I'm not 100% sure if rand can take decimals Otherwise you can go RAND(1,5)/100.

Re: Random Percentages

Posted: Thu Oct 09, 2014 10:16 pm
by Xaos
Yeah you can do this any number of ways. Just do rand(1,100)/100 for the decimal, or rand(1,100) and then echo a % sign, depending on what you want to do.

Re: Random Percentages

Posted: Thu Oct 09, 2014 10:48 pm
by Epiales
Xaos wrote:Yeah you can do this any number of ways. Just do rand(1,100)/100 for the decimal, or rand(1,100) and then echo a % sign, depending on what you want to do.
What do you mean echo a % sign? I've been up for 36 hours lol... So what you said doesn't make sense...

Code: Select all

		$atk_player2 = rand(1,2)/100 * $_POST['dropdown'] * $attack_user['defense'];
		echo '$atk_player2%';
That's all I can think of, but that doesn't work :mrgreen: :mrgreen:

What I really want to do I think, is to take the number of bullets they use and multiply that by the percent + their defense or something

So if they chose

1 bullet, it would be 1% * defense
2 bullet, it would be 2% * defense
3 bullet, it would be 3% * defense

ect....

Re: Random Percentages

Posted: Thu Oct 09, 2014 10:56 pm
by Epiales
Or my original thought....

if they choose between 1 and 5 bullets, h ave it randomly pick a % of the players health
if they choose between 6 and 10 bullets, have it reandomly pick a % of the players health

So something like it would be between a random 1% to 5% loss of the players health for so many bullets used... the more the higher percentage.

Re: Random Percentages

Posted: Thu Oct 09, 2014 11:41 pm
by Sim
% is based on 100% so

Code: Select all

$var = rand(1, 100);
this will give you a random number 1 - 100.

if the number is 8. Its 8%
if the number is 3. Its 3% and jackpot.


Follow?

Re: Random Percentages

Posted: Thu Oct 09, 2014 11:47 pm
by Sim
I guess I misread what was going on here.

1-5 random number

Based on what Xaos said.

Code: Select all

      
$rand_num = rand(1,5);
$atk_player2 = ($rand_num /100) * $_POST['dropdown'] * $attack_user['defense'];
echo "$atk_player2%";
echo "Your percentage ejaculation bonus was $rand_num%";

Re: Random Percentages

Posted: Thu Oct 09, 2014 11:59 pm
by Epiales
Sim wrote:I guess I misread what was going on here.

1-5 random number

Based on what Xaos said.

Code: Select all

      
$rand_num = rand(1,5);
$atk_player2 = ($rand_num /100) * $_POST['dropdown'] * $attack_user['defense'];
echo "$atk_player2%";
echo "Your percentage ejaculation bonus was $rand_num%";
Okay, I end up with two percentages. Which one is what?

Code: Select all

1.2%Your percentage ejaculation bonus was 4%
Sorry I'm not understanding it. Under other circumstanses here at home, I might get it lol.

Re: Random Percentages

Posted: Fri Oct 10, 2014 12:47 am
by Epiales
Okay, I have this code written below now:

Code: Select all

<?php
$rand_num = rand(1,2);
$atk_player = ($rand_num /100) * $_POST['dropdown'] * $row['defense'];
$atk_player2 = round($atk_player);
echo "$atk_player2%";
echo "Your percentage ejaculation bonus was $rand_num%";

$updatehpoints = "UPDATE users SET hpoints=hpoints - $atk_player2 WHERE username = '$_SESSION[username]'";
$query = mysqli_query($db_conx, $updatehpoints);
}}
?>
It showed that it was 6% one time but it was really .6%

Another time it was 12% but it was really 1.2%

How can I fix that accordingly? So it says it's 1.2% health loss instead of the 12 % that is isn't?

Thank you so very, very much for helping here. :mrgreen:

Re: Random Percentages

Posted: Fri Oct 10, 2014 1:57 am
by Xaos
12% = .12. So in doing the math and formulas you would use .12, but in displaying to user, you would say

Code: Select all

echo "You lost " . $rand . "% of your health"';