#1
  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. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    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.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    3
    Rep Power
    0
    yeah, thank you
    i got
  8. #5
  9. 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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    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.

IMN logo majestic logo threadwatch logo seochat tools logo