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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vegtard
    New Member
    • May 2007
    • 7

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

    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 :)
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by vegtard
    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:
    Source code: Lib/random.py This module implements pseudo-random number generators for various distributions. For integers, there is uniform selection from a range. For sequences, there is uniform s...

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by vegtard
      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.

      Comment

      • Joshua Brooks
        New Member
        • Dec 2010
        • 3

        #4
        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 += 1
        diceroll.sort() #sort lowest to highest
        diceroll = diceroll[1:] #drop the lowest number
        print diceroll

        Comment

        • Sean Pedersen
          New Member
          • Dec 2010
          • 30

          #5
          Joshua and others are right; of course it can be done! I've modified Joshua's code a bit...
          Code:
          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

          Comment

          Working...