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.

Random Percentages

Postby Epiales » Thu Oct 09, 2014 2:17 pm

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
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Random Percentages

Postby Sharlenwar » Thu Oct 09, 2014 2:54 pm

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**
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
User avatar
Sharlenwar
 
Posts: 506
Joined: Mon May 28, 2012 2:14 pm
Has thanked: 3 times
Been thanked: 4 times
Blog: View Blog (2)

Re: Random Percentages

Postby Xaos » Thu Oct 09, 2014 5:16 pm

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
Xaos
 
Posts: 946
Joined: Tue Jan 10, 2012 11:01 pm
Location: Monroe, Georgia
Has thanked: 5 times
Been thanked: 8 times

Re: Random Percentages

Postby Epiales » Thu Oct 09, 2014 5:48 pm

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: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Random Percentages

Postby Epiales » Thu Oct 09, 2014 5:56 pm

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
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Random Percentages

Postby Sim » Thu Oct 09, 2014 6:41 pm

% 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 12:37 pm
Location: Baltimore
Has thanked: 39 times
Been thanked: 13 times
Blog: View Blog (1)

Re: Random Percentages

Postby Sim » Thu Oct 09, 2014 6:47 pm

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
Sim
 
Posts: 412
Joined: Sat Dec 26, 2009 12:37 pm
Location: Baltimore
Has thanked: 39 times
Been thanked: 13 times
Blog: View Blog (1)

Re: Random Percentages

Postby Epiales » Thu Oct 09, 2014 6:59 pm

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: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Random Percentages

Postby Epiales » Thu Oct 09, 2014 7:47 pm

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
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Random Percentages

Postby Xaos » Thu Oct 09, 2014 8:57 pm

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"';
User avatar
Xaos
 
Posts: 946
Joined: Tue Jan 10, 2012 11:01 pm
Location: Monroe, Georgia
Has thanked: 5 times
Been thanked: 8 times

Next

Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

x