Reputation: 1152
I'm making a game and I would like make my char's damage range(4,7),
To inflict damage, im doing enemyhp - chardamage, How would I make chardamage a random number from the range(4,7)
?
Upvotes: 0
Views: 300
Reputation: 3808
import random
damage = random.randint(4, 7) # To get random num from {4,5,6,7}
Upvotes: 2
Reputation: 992887
You can do this using random.randrange
:
random.randrange(4, 8)
You need to use 8 because in Python, the range is inclusive of the lower bound and exclusive of the upper bound.
Upvotes: 5
Reputation: 174624
You need range(4,8)
because the upper bound is always -1. range(4,7)
will give you 4,5,6
from random import choice
choice(range(4,8))
Upvotes: 2
Reputation: 113950
import random
print random.randint(4,7)
....
if you want floats then
print random.uniform(4,7)
Upvotes: 2