ActiveState Code

Recipe 522978: Dice Roll


An object oriented approach to rolling a custom dice! this technique allows a person to define a custom dice by list, by integer, or by nothing at all defaulting to 6 sides.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from random import choice as _choice

class die:
  def __init__(self, sides=6):
    try: self.sides = range(1,sides+1)
    except TypeError: self.sides = list(sides)
  
  def __str__(self):
    return str(_choice(self.sides))

'''
#to roll
#default
print die()
#custom number
print die(12)
#custom sides
print die(('yes','no','maybe'))
#changing die
changing=['something','something else','also something']
print die(changing)
changing+=['something new!']
print die(changing)
'''

Discussion

is there something similar to __str__ for class' that will return anything and not just a string??

Comments

  1. 1. At 10:05 a.m. on 22 jun 2007, Tony Blundell said:

    Hi

    By storing the sides as a list, equal to range(1, sides + 1) when the die is initiated with an integer, you can eliminate the if statement in the roll method...

    from random import randrange as _rand

    class die():

    def __init__(self,sides=6):
    
        if (type(sides)==int):
    
            self.s = sides
    
            self.sl = range(1, sides + 1)
    
        else:
    
            self.s = len(sides)
    
        self.sl = sides
    
    
    
    
    def roll(self):
    
        return self.sl[_rand(0, self.s)]
    

    Cheers

    Tony

  2. 2. At 10:08 a.m. on 22 jun 2007, Tony Blundell said:

    BTW, is there a neater way to put code into comments? :-)

  3. 3. At 8:45 a.m. on 23 jun 2007, Brandon Beck said:

    Use random.choice() instead? What's wrong with using the random.choice method? Seems like it was built for exactly this situation.

    Also I tend to try to employ the "it's easier to ask forgiveness than permission" principle when possible to avoid things like type checking. So initially assume the sides parameter is an integer. If that assumption fails then assume it's something iterable. If that fails then bubble the exception back up to the user.

    from random import choice
    
    class die:
      def __init__(self, sides=6):
        try: self.sides = range(sides)
        except TypeError: self.sides = list(sides)
    
      def roll(self):
        return choice(self.sides)
    
  4. 4. At 4:30 p.m. on 30 jun 2007, Symon Polley (the author) said:

    thanks for the comment! thanks for looking it over, that does look like a better way to approach this algorithm!

  5. 5. At 1:50 a.m. on 8 jul 2007, Symon Polley (the author) said:

    quick correction. if anyone plans to use Brandons code, change range(sides) to range(1,sides+1), the default of range includes 0 and only goes up to sides, not including it. so the code should be:

    from random import choice

    class die: def __init__(self, sides=6): try: self.sides = range(1,sides+1) except TypeError: self.sides = list(sides)

    def roll(self): return choice(self.sides)

  6. 6. At 1:56 a.m. on 8 jul 2007, Symon Polley (the author) said:

    oops. i meant

    if anyone plans to use Brandons code,

    change range(sides) to range(1,sides+1),

    the default of range includes 0 and only goes up to sides,

    not including it. so the code should be:

    from random import choice
    
    class die:
       def __init__(self, sides=6):
          try: self.sides = range(1,sides+1)
          except TypeError: self.sides = list(sides)
       def roll(self):
          return choice(self.sides)
    
  7. 7. At 2:02 a.m. on 8 jul 2007, Symon Polley (the author) said:

    yep. you paste your code in and use a little html code to display it properly:

    pre "your code" /pre

    except surround the pre and /pre with a less than symbol on the left and a greater than symbol on the right...

    and thats it!

  8. 8. At 2:05 a.m. on 8 jul 2007, Symon Polley (the author) said:

    honestly. i hate the way comments are handled on this site... i cant even delete a comment that i wrote... and you cant put returns or tabs in without using the limited html code they allow... not a very good way to go about it... although i do like nested comments!

  9. 9. At 9:16 p.m. on 9 jul 2007, Symon Polley (the author) said:

    theif. alright i'm stealing your code, and modifying it a bit haha i hope you dont mind, i'll credit you or remove it if you would like! just let me know!

Sign in to comment