user4139413
user4139413

Reputation:

Program that simulates rolling a dice, and tells you how many times you roll each number

I am a beginner programer, and I have to create a program that simulates rolling a dice 10000 times, and then tells the user how many times they rolled each number. The hard part is, I have to do it using only two variables.

import random
count1=0
count2=0
count3=0
count4=0
count5=0 
count6=0
dice=random.randint(1,7)
for i in range(10000):
    if dice==1:
        count1+=1
    if dice==2:
        count2+=1
    if dice==3:
        count3+=1
    if dice==4:
        count4+=1
    if dice==5:
        count5+=1
    if dice==6:
        count6+=1
print "You entered "+ str(count1)+ " ones, " + str(count2) + " twos, "+str(count3) + " threes, " +str(count4)+ " fours, " +str(count5)+ " fives, and "+str(count6) +" sixes."

The problem is, I can't get the program to pick more than one random number, it will just repeat the same number 10000 times. Also, as you can see, I have no idea how to write this program with only two variables, but I think it may have something to do with lists.

Upvotes: 0

Views: 15605

Answers (5)

MOHSEN_HOSEINI
MOHSEN_HOSEINI

Reputation: 3

import random
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0

for i in range(10000):
    dice = random.randint(1, 7)
    if dice == 1:
        one += 1
    if dice == 2:
        two += 1
    if dice == 3:
        three += 1
    if dice == 4:
        four += 1
    if dice == 5:
        five += 1
    if dice == 6:
        six += 1

print(one, two, three, four, five, six)

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881113

Your immediate problem is that you get a random value once before the loop starts, and then use that single value each time through the loop. To fix this, the call to random.randint() should be moved inside the loop:

for i in range(10000):
    dice=random.randint(1,7)
    if dice==1:

Secondly, the call as you have it will give you numbers between one and seven inclusive which, unless you're using some weird Dungeons and Dragons style dice, is probably not what you want. You should be generating numbers from one to six inclusive or, as per below with the arrays, zero through five.

Thirdly, if you're limited to two variables, it's almost a guarantee that you'll use those up with a loop counter and an array of count values, leaving nothing left over for the dice throw and individual count variables.

So you'll need something like the following pseudo-code:

dim count[1..6]
for i = 1 to 6 inclusive:
    count[i] = 0
for i = 1 to 10000 inclusive:
    count[random(1..6)] += 1
for i = 1 to 6 inclusive:
    output "Dice value ", i, " occurred ", count[i], " times."

If this is classwork, I urge you to stop reading now and implement your own solution based on that. If it's not classwork, or your ethics aren't as strong as they could be (a), the Python code below shows one way of doing it, keeping in mind that Python arrays are zero-based rather than one-based:

import random

count = [0, 0, 0, 0, 0, 0]

for i in range(10000):
    count[random.randint(0,5)] += 1

for i in range(6):
    print "Value %d happened %d times" % (i + 1, count[i])

(a) Just be aware that SO is a fairly well known site and your educators may well be checking classwork against things they find on the net.

Upvotes: 1

nishparadox
nishparadox

Reputation: 770

you can use a dictionary:

import random
from collections import defaultdict
dice = defaultdict(int)
for x in range(10000):
    dice[random.randint(1,6)] += 1
print(dice)

Upvotes: 0

Stephen Lin
Stephen Lin

Reputation: 4912

Because you put dice=random.randint(1,7) outside the loop which of course will always only generate one number.

I will put code for 'only two variables' later. Looks like an interesting question.

Upvotes: -1

101
101

Reputation: 8979

You need to put the line dice=random.randint(1,7) inside the for loop.

Upvotes: 1

Related Questions