3

I have a text adventure game with battle system in place already. It is of course turn based, both player and enemy deal damage points (in float). I use random.uniform() function to generate attack points for every round. Zero stands for missing an enemy or player. However since I use floats that chance is pretty slim. I want to create a condition where there would be let's say a chance 1 in 5 that attack points equal 0.00. This condition must be run on every round.

How would I go about it? Is there a module I could use?

note: I think I could use random module (random.randint(0,5)) but I was wondering if there is some other way. Thanks!

2
  • Should your attack points be floating point values or integers? Commented Mar 24, 2014 at 11:51
  • Both hitpoints and attack points are floats. Maybe I'm just complicating things... I've tried creating conditions with random.randint(0,4) and it works pretty good. But I'm still interested in other solutions. Commented Mar 24, 2014 at 11:55

4 Answers 4

3

I think you should use random.uniform() reserving 20% of values for 0.00 attack points.

hit = random.uniform(0, 10)
if hit <= 2:
    damage = 0.00
else:
    damage = (hit - 2) / 8

This is just a stupid example but might help. Or even more simple:

hit = random.uniform(0, 10)
if hit >= 8:
    damage = 0.00
else:
    damage = hit / 8
Sign up to request clarification or add additional context in comments.

1 Comment

Aaah, I see. Simple mathemathics - should have done this in the first place. Thanks!
1

I'd suggest rolling two random numbers: One for the chance to hit and another for the actual damage dealt. You could also combine them with player attributes. Example:

if random.uniform(0, 100) < player.chance_to_hit:
    damage = random.uniform(weapon.min_damage, weapon.max_damage)

1 Comment

Yeah I ended up with something like this actually.
0

You can multiply a chance going from 0 to 1 (float) by a maximum hit damage (int):

try:
    chance = 1.0 / random.randint(0, 5)
except ZeroDivisionError:
    chance = 0.0

damage = chance * MAXIMUM_DAMAGE

Just a guess.

Comments

0

a chance 1 in 5 that attack points equal 0.00.

if random.randint(1, 5)==1:
    attack_points=0.00

|

or if you want to see big true-like numbers:

if random.randint(0, 100)<=20:
    attack_points=0.00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.