Random Percentages

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Random Percentages

Post 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?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Sharlenwar
Posts: 524
Joined: Mon May 28, 2012 7:14 pm

Re: Random Percentages

Post 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.
Deep within the Void of Quasion, a creation.

**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Random Percentages

Post 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.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Random Percentages

Post 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....
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Random Percentages

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Random Percentages

Post 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?
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Random Percentages

Post 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%";
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Random Percentages

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Random Percentages

Post 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:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Random Percentages

Post 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"';
Post Reply

Return to “Beginner Help and Support”