0

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)?

3
  • 3
    Did you try Googling? I found random.randint in the first result. Commented Sep 26, 2012 at 4:03
  • 2
    python has a pretty good introspection features. And often times you can stumble on what you need. From the python CLI, try import random, and then dir(random). The if something looks promising try help(random.interestingthing). Commented Sep 26, 2012 at 4:07
  • Do you mean Python's range(4, 7) (== [4, 5, 6]), or did you mean [4, 5, 6, 7]? Commented Sep 26, 2012 at 23:32

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2
import random
print random.randint(4,7)

....

if you want floats then

print random.uniform(4,7)

Comments

2

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))

1 Comment

Python actually lets you use xrange there (for Python 2 users), and gives the right results :)
2
import random

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

1 Comment

Note that randint() behaves differently from randrange(), and you would need to use randint(4, 7) to get a random integer from {4, 5, 6, 7}.

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.