5 Replies - 2833 Views - Last Post: 03 November 2013 - 03:45 AM Rate Topic: -----

#1 ProgramKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-October 13

simulate rolling dice

Posted 31 October 2013 - 08:56 PM

You will write a program to simulate the rolling of a pair of dice.

- You will ask the user for the number of rolls to simulate. You will then roll two dice per roll. Use the random library and the randint function therein (random.randint(1,6)) for each dice.

- Add the numbers from each dice, and keep a count of each possible roll (2-12), and how many times you roll “doubles” (the same number on both dice).

- Print out each dice count, the number of rolls for that dice count, and the percentage of the total:



Enter the number of rolls: 10000



2 - 266 2.660000%

3 - 580 5.800000%

4 - 881 8.810000%

5 - 1086 10.860000%

6 - 1418 14.180000%

7 - 1658 16.580000%

8 - 1363 13.630000%

9 - 1096 10.960000%

10 - 829 8.290000%

11 - 550 5.500000%

12 - 273 2.730000%



Doubles - 1425 - 14.234000%

This is the start of my code.
I may be doing this completely wrong.
any feedback helps!
Very lost!

import random #randint
def main():
    rolls = input("Enter the number of rolls:")
    total2 = 0
    total3 = 0
    total4 = 0
    total5 = 0
    total6 = 0
    total7 = 0
    total8 = 0
    total9 = 0
    total10 = 0
    total11 = 0
    total12 = 0
    t2 = 0
    t3 = 0
    t4 = 0
    t5 = 0
    t6 = 0
    t7 = 0
    t8 = 0
    t9 = 0
    t10 = 0
    t11 = 0
    t12 = 0
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    randomNumGen(dice1, dice2, total2, total3, total4, total5, total6, total7, total8, total9, total10, total11, total12)
    text(t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12)
def randomNumGen(dice1, dice2, total2, total3, total4, total5, total6, total7, total8, total9, total10, total11, total12):
    if dice1 + dice2 == 2:
        t2 = total2 + (dice1 + dice2)
        return t2
    elif (dice1 + dice2) == 3:
        t3 = total3 + (dice1 + dice2)

        return t3
    elif (dice1 + dice2) == 4:
        t4 = total3 + (dice1 + dice2)

        return t4
    elif (dice1 + dice2) == 5:
        t5 = total3 + (dice1 + dice2)

        return t5
    elif (dice1 + dice2) == 6:
        t6 = total3 + (dice1 + dice2)

        return t6
    elif (dice1 + dice2) == 7:
        t7 = total3 + (dice1 + dice2)

        return t7
    elif (dice1 + dice2) == 8:
        t8 = total3 + (dice1 + dice2)

        return t8
    elif (dice1 + dice2) == 9:
        t9 = total3 + (dice1 + dice2)

        return t9
    elif (dice1 + dice2) == 10:
        t10 = total3 + (dice1 + dice2)

        return t10
    elif (dice1 + dice2) == 11:
        t11 = total3 + (dice1 + dice2)

        return t11
    elif (dice1 + dice2) == 12:
        t12 = total3 + (dice1 + dice2)

        return t12

def text(t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12):
    print("2 - ", t2)
    print("3 - ", t3)
    print("4 - ", t4)
    print("5 - ", t5)
    print("6 - ", t6)
    print("7 - ", t7)
    print("8 - ", t8)
    print("9 - ", t9)
    print("10 - ",t10)
    print("11 - ", t11)
    print("12 - ", t12)
    
    
main()




Is This A Good Question/Topic? 0
  • +

Replies To: simulate rolling dice

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7654
  • View blog
  • Posts: 12,909
  • Joined: 19-March 11

Re: simulate rolling dice

Posted 31 October 2013 - 10:09 PM

First of all: the singular of "dice" is "die". Please inform your teacher.

Now, for the code. One major flaw: it seems that you're only rolling the dice once. How are you getting these numbers?

Also this:
t2 = total2 + (dice1 + dice2)


is totally wrong. You want to count the number of times that you reached a given roll. This sums up the number of rolls that total a given number. (so this gives you 6 times the number of times you rolled a six, for example)

As for structure, have you heard of lists? Any time you find yourself using numbered variables to keep track of different versions of the same thing, you're doing it wrong.

Use a single list to track the totals for each possible result. (there are 11 possible results, but if you use a list of length 13, you can index into it using the actual totals, and save yourself some conversions)

This post has been edited by jon.kiparsky: 31 October 2013 - 10:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: simulate rolling dice

Posted 01 November 2013 - 09:05 AM

This guy seems to be cross posting on a couple of websites.

http://yhoo.it/Ht0z0v
http://yhoo.it/Ht0x8N
http://bit.ly/Ht0qKf
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5249
  • View blog
  • Posts: 27,073
  • Joined: 10-May 07

Re: simulate rolling dice

Posted 01 November 2013 - 09:07 AM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#5 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: simulate rolling dice

Posted 03 November 2013 - 03:23 AM

Quote

This guy seems to be cross posting on a couple of websites.


Well, I'm pretty sure that's OK for people to do.

Anyway, I'm learning python myself and I did your problem. A few things that I noticed you could run into are:

To print that percent sign and still use a formatted print statement, use double percent sign: print "Doubles - %d - %.2f %%" % (...)

Next, you have to do division so be sure that you're minding your values as floats: total2 = 0.0 or you will lose the remainders.

Personally, I went with a dictionary to do this. I will show you a little bit since this post is a few days old and you most likely have solved this problem by now.

# list of numbers and their totals:
outcomes = {
    2 : 0.0,
    3 : 0.0,
    4 : 0.0,
    5 : 0.0,
    6 : 0.0,
    7 : 0.0,
    8 : 0.0,
    9 : 0.0,
    10: 0.0,
    11: 0.0,
    12: 0.0
}



Then I just access the total by its roll and increment it:

# add the dice:
total = d1 + d2

# increment the dictionary entry:
outcomes[total] += 1



Then to access it with prints, I probably do something wrong here but it works:

# print each dice total:
for i in range(0, len(outcomes)):
    print "%d - %d - %.2f %%" % (i, outcomes[i+2], \
            (outcomes[i+2] / desired_rolls) * 100)



So, i access the outcomes dictionary from the current iteration + 2 since we start at key 2 in dictionary but index 0. It's a little complex if you're new to programming.

Oh and since there is some weird formality of python code only being like 80 characters long, you can do line continuation with the \ character which is nice.

Hope you have fun in your class.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: simulate rolling dice

Posted 03 November 2013 - 03:45 AM

View PostMentalFloss, on 03 November 2013 - 10:23 AM, said:

Quote

This guy seems to be cross posting on a couple of websites.


Well, I'm pretty sure that's OK for people to do.

It is preferred that an OP confirm that they have cross-posted, so that someone doesn't waste time on a question that has already been answered.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1