### Thread: need help with dice

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2005
Posts
3
Rep Power
0

#### need help with dice

i need to simulate the rolling of a pair of dice with a counter loop that will simulate five rolls.
2. Originally Posted by sammyhappysoc
i need to simulate the rolling of a pair of dice with a counter loop that will simulate five rolls.
OK, here are some hints:
• to simulate the dice, you'll need one of the functions in the random library, specifically one which will generate an integer between two integer values, inclusive of those values. There is a specific function which you'll find will do precisely this.
• You'll want to simulate each of the two dice separately, as a pair of dice has a bell distribution rather than a linear distribution. Also, it would allow you to display the individual die values, which I assume you need to do for the assignment anyway.
• While a while: loop or a recusive call could be used for the counter loop, the most appropriate way to perform one in Python is with the for: statment, which is intended for that purpose. See this thread for a few examples of how to use the expression.

Oh, and if you haven't already, I would recommend reading the New Members Introduction and the Tips and Tricks threads, including the external links. HTH.
Last edited by Schol-R-LEA; April 4th, 2005 at 05:24 AM.
3. A nice step by step Shol', one thing I would add to that would be to make the dice a function to increase code reusability.

I have a Dice role example; it's not exactly what you want but it's a start. If you post some code so we can see how it's going I'll post the example.

Take care,

Mark.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2005
Posts
3
Rep Power
0
yeah, thank you
i got
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2005
Posts
3
Rep Power
0
here is the code, i made

import random

random.seed()

roll = 1
counter = 0

while(counter < 5):

die1 = random.random()
die2 = random.random()

dice1 = int((6*die1)+1)
dice2 = int((6*die2)+1)

print "Roll", roll, ":", "Die 1 = ", dice1, "and",
print "Die 2 = ", dice2,

total = dice1 + dice2

print "Total is", total

roll = roll + 1
print

counter = counter + 1

print
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2005
Posts
78
Rep Power
14
Originally Posted by sammyhappysoc
here is the code, i made

import random

random.seed()

roll = 1
counter = 0

while(counter < 5):

die1 = random.random()
die2 = random.random()

dice1 = int((6*die1)+1)
dice2 = int((6*die2)+1)

print "Roll", roll, ":", "Die 1 = ", dice1, "and",
print "Die 2 = ", dice2,

total = dice1 + dice2

print "Total is", total

roll = roll + 1
print

counter = counter + 1

print
1/ You should be using random.randint() rather than random.random()
2/ You really don't need random.seed() in a program like this.
3/ Use augmented assignment, eg: roll += 1
4/ enclose code in code tags, which will preserve formatting (there's a button up above if you don't know how to do it manually)
5/ use a for loop with range() rather than a while loop and explicit counter
6/ For that matter, you don't need a separate roll and counter tally... they're the same thing if you make your range from 1 to 6 rather than 0 to 5.
7/ Don't have a comma-terminated print followed immediately by a blank print. Just have a non-comma-terminated print.
8/ Your print statements will be prettier if you use string formatting (look it up) - eg:
Code:
```x, y = 1, 2
print "My numbers are %s and %s."%(x, y)```
Your finished code should be a maximum of five lines of code plus some print statements and blank lines.

--OH.