user1692517
user1692517

Reputation: 1152

Choose number at random from a range of numbers - Python

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

Answers (4)

Marc Cohen
Marc Cohen

Reputation: 3808

import random

damage = random.randint(4, 7) # To get random num from {4,5,6,7}

Upvotes: 2

Greg Hewgill
Greg Hewgill

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

Burhan Khalid
Burhan Khalid

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

Joran Beasley
Joran Beasley

Reputation: 113950

import random
print random.randint(4,7)

....

if you want floats then

print random.uniform(4,7)

Upvotes: 2

Related Questions