By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

How do you simulate a dice roll in python?

P: 4
I'm writing a text based adventure game and was wondering if there was a way to "Roll Dice". An example of what i am trying to use this for would be attacking or defending in the game. If they get above or equal to a certain number It would be considered a hit. I would also need this same automated dice rolling for the enemies in the game.

Thanks

Matt.
Dec 6 '10 #1

✓ answered by Joshua Brooks

Sounds like a random number generator is what you need. 'randrange' gives a one-liner to get a random integer:

from random import randrange
diceroll = randrange(1,7)

Share this Question
Share on Google+
3 Replies


P: 3
Sounds like a random number generator is what you need. 'randrange' gives a one-liner to get a random integer:

from random import randrange
diceroll = randrange(1,7)
Dec 6 '10 #2

Sean Pedersen
P: 30

P: 4
Thanks a lot. Both were a big help.
Dec 6 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.