-4

I making a fighting game. Its like there are two players. They have 100 hp and they choose attack value btween 0-100 when they're fighting. After they choose value they should have %(100-value) chance of being successful with this attack. For example if they choose value 60, they have %40 chance to be successful with this attack.Can you please help. Sorry for my bad english.

  • So if they choose `0`... – smac89 Dec 21 '17 at 00:01
  • I think you don't want to have them choose, no? Maybe you want to choose for them? So `import random; print random.random() * 100`. Even better `import random; print random.randint(1, 100)` – smac89 Dec 21 '17 at 00:01
  • So your question is, "how does the `random` module work"? That's too much to answer in one question. – AJF Dec 21 '17 at 00:02
  • 1
    I'd suggest you try your best to do what you need and come back when you have some specific question. You can take the [random](https://docs.python.org/3/library/random.html) module and run it through a translator, which will help you understand the module in a general sense. – Paul Rooney Dec 21 '17 at 00:08
  • Possible duplicate of [Generate random integers between 0 and 9](https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9) – smac89 Dec 21 '17 at 00:12
  • @smac89 I see no problem in an attack doing no damage succeeding 100% of the time. – Thijs van Dien Dec 21 '17 at 00:20
  • @ThijsvanDien exactly my point. What would be the point of having them choose if they would always choose 0 anyways? – smac89 Dec 21 '17 at 00:24
  • 1
    @smac89 If you pick a low value, you have a weak attack with a high chance of succeeding. If you pick a high value, you have a strong attack with a low chance of succeeding. Both extremes—an attack that always succeeds but does no damage, or an attack that does maximum damage but never succeeds—are useless, so I don't see why anyone would pick either. Still, choosing between high damage + high risk or low damage + low risk does make sense, based on how lucky you're feeling. :) – Thijs van Dien Dec 21 '17 at 00:28

2 Answers2

2

What you want is to generate a random number between 0 and 100 and check if it's greater than your inserted number.

For example, if you enter 50, there's a 50% chance the random number will be greater (or equal), 50% lower. If you enter 10, there's a 90% chance the number will be greater.

You can use random.randrange(101) to generate a random number between 0 and 100 inclusive. Then it's a simple comparison to your value, and that should be all you need.

SCB
  • 5,821
  • 1
  • 34
  • 43
1

Generally, in statistics, a chance is expressed as a number between 0 and 1, so 0.6 represents a 60% chance. Then, if you want to have a 0.6 chance of success, you can determine success or not by picking a random floating point number between 0 and 1 and check if it is smaller than or equal to 0.6.

Intuitively, you can look at it this way:

|-----------------------------------|-----------------------|
0                                  0.6                      1

If you'd pick a random point on this line—your random number between 0 and 1—the chance of it being on the left side of the 0.6 is... 0.6 (60%).

You can use random.uniform for this:

import random
success_rate_percentage = 60
success = random.uniform(0, 1) <= success_rate_percentage / 100.0

Now as a simple check if this is functioning properly:

>>> import random
>>> success_count = 0
>>> for _ in range(100000):
...     if random.uniform(0, 1) <= 0.6
...         success_count += 1
...
>>> success_count
59954

The expected result is about 0.6 * 100000 = 60000 successes and the actual result is 59954, so that's close enough.

Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48