Rolling the dice

Almost everyone has played a game using dice. Whether it's Monopoly, Yahtzee, Trouble, Backgammon, or some other game, rolling dice is one of the most common ways to generate a random event in a game.

Dice are very easy to simulate in a program, and Python's random module has a couple of ways to do it. One is the randint() function, which picks a random integer. Because the number of spots on the sides of a die are integers (1, 2, 3, 4, 5, and 6), rolling a single die could be simulated like this: import random die_1 = random.randint(1, 6)

That gives you a number from 1 to 6, with each number having an equal chance of appearing. This is just like a real die.

Another way to do the same thing is to make a list of the possible results, and then use the choice () function to pick one of them. Here's how that would look: import random sides = [1, 2, 3, 4, 5, 6] die 1 = random.choice(sides)

This would do exactly the same thing as the previous example. The choice() function randomly chooses an item from a list. In this case, the list is the numbers from 1 to 6.

100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook

Post a comment