A simple function to permit you to generate random numbers by emulating a dice roll. The number of dice and the number of sides to each die are the parameters of the function. In order to (for example) roll 4d6, you would call dice( 4, 6 ).
def dice( num, sides ): return reduce(lambda x, y, s = sides: x + random.randrange(s), range( num + 1 )) + num
Simulating a dice roll is a good way to generate a random number with an expected profile. For example, 3d6 will generate a bell-shaped probability curve with an average of 10.5.
After trying a more manual approach (for loop with an accumulator), I found that using reduce is generally faster. It's possible this implementation could be faster still, as I haven't profiled it very aggressively; it's fast enough for my purposes :)