from random import randrange
import math
 
   
def main():
    
    print "This program will simulate  the  probability of rolling "
    print " a 5 of a knid in one throw."
    pos = randrange(1,7) 
    
    dice = [0] *5
    
    value = dice [ : ]
      
      
    for pos in [0,1,2,3,4]:
       dice[pos] = randrange(1,7)
       roll = dice[pos]
       print roll
     
       
        
    counter = 0
    counts = [0] * 7
    for value in dice[ : ]:
        counts[value] = counts[value] + 1
        while counts != 5:
            counter = counter + 1
        else:
            print counter

This is what I have so far to try to get my dice to roll I know I am missing something but I 've got writer's block can you assist?

Recommended Answers

All 7 Replies

I am not sure if this is what you want to accomplish, but I HTH:

import random

def roll_dice(stop=100):
    # roll five dice and summarize the occurrances of duplicate numbers
    
    cnt = 1
    dd = {}
    
    while cnt <= stop:

        dice = [random.randrange(1,7) for _ in range(5)]
        
        # eliminate duplicates, Python 2.3
        dice1 = [i for i in dice if i not in locals()['_[1]'].__self__]
        if len(dice1) == 1:
            print dice
        
        try:
            dd[len(dice1)] = dd[len(dice1)]+1
        except:
            dd[len(dice1)] = 1
        cnt += 1
        
    return dd

dd = roll_dice(1000)

>>> [2, 2, 2, 2, 2]
[2, 2, 2, 2, 2]
>>> dd
{1: 2, 2: 95, 3: 482, 4: 384, 5: 37}
>>>

liz517, could you please explain what your code should do.

It is supposed to take 5 dice and roll them, and then count the value in each position to determine if they are all equal. for instance [1,1,1,1,1,] or [2,2,2,2,2].

from random import randint

def roll( num ):
    # this counts the "5 of a kind"
    hit = 0
    # we try this num times
    for i in range(num):
        # roll the dices
        dices = [ randint(1, 7) for _ in range(5) ]
        # check if all dices have the same value
        if len(set(dices)) == 1:
            hit += 1
    return hit

tries = 100000

print "%s %%" % ( roll( tries ) / float(tries) * 100 )

Here is how set() works:

In [1]: set( [1, 1, 2, 2, 3, 3] )
Out[1]: set([1, 2, 3])

In [2]: set( [1, 1, 1, 1, 1] )
Out[2]: set([1])

Hope this helps.

cool

yeah

you probably know c or c++ better than you know python. you forgot to do main() at the end. Python doesn't automatically run it.

commented: This thread was over 2 years old... check the date before posting! -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.