Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Python module and creating chances

Tags:

python

random

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!

like image 302
user3056783 Avatar asked Jun 30 '26 18:06

user3056783


2 Answers

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
like image 87
ElmoVanKielmo Avatar answered Jul 03 '26 07:07

ElmoVanKielmo


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)
like image 40
tobias_k Avatar answered Jul 03 '26 08:07

tobias_k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!