449,283 Members | 1,254 Online
Need help? Post your question and get tips & solutions from a community of 449,283 IT Pros & Developers. It's quick & easy.

# "rolling a dice" in python. can it be done?

 P: 7 simple question. is it possible to write a script that rolls a six sided dice? if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score? if you want to know, i am working on a script to create a dungeons and dragons character :) May 28 '07 #1
4 Replies

 Expert 100+ P: 844 simple question. is it possible to write a script that rolls a six sided dice? if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score? if you want to know, i am working on a script to create a dungeons and dragons character :) Ofcourse it is possible, python can do anything! You can use the random module. it has several functions having to do with anything random. Try this link: http://docs.python.org/lib/module-random.html May 28 '07 #2

 Expert 5K+ P: 6,596 simple question. is it possible to write a script that rolls a six sided dice? if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score? if you want to know, i am working on a script to create a dungeons and dragons character :) Just search TheScripts for "python dice". There are plenty of examples of this solution. May 28 '07 #3

 P: 3 Python. Of course it's possible :) No claims that this is efficient/pretty/best practise it just does the job. from random import randrange diceroll = [0, 0, 0, 0] #initialise a holder for dice rolls i = 0 #counter for number of rolls while i < 4: #loop through a dice throw routine diceroll[i] = randrange(1,7) i += 1diceroll.sort() #sort lowest to highest diceroll = diceroll[1:] #drop the lowest number print diceroll Dec 6 '10 #4

 P: 30 Joshua and others are right; of course it can be done! I've modified Joshua's code a bit... Expand|Select|Wrap|Line Numbers diceroll = [] #initialise a holder for dice rolls i = 0 #counter for number of rolls for i in range(4): #loop through a dice throw routine     diceroll.append(randrange(1,7))   diceroll.sort() #sort lowest to highest diceroll = diceroll[1:] #drop the lowest number print diceroll  Dec 6 '10 #5