samedi 31 octobre 2015

Distribution of dice rolls

from random import randrange

def roll2dice() -> int:
    roll2 = []
    for i in range(50):
        sum = randrange(1,7) + randrange(1,7)
        roll2.append(sum)
    return roll2

The above function is for generating the random rolling sum of two die.

def distribution (n: int):
    result = []
    for x in range(2,13):
        result.append(roll2dice())
    for x in range(2,13):
        dice = result.count(x)
        num_of_appearances = result.count(x)
        percentage = (result.count(x) / int(n)) * 100
        bar = result.count(x)*'*'
    print("{0:2}:{1:8}({2:4.1f}%)  {3.5}".format(dice, num_of_appearances, percentage, bar))

I then used roll2dice to create a distribution function in which

distribution(200)

should yield:

 2:     7 ( 3.5%)  *******
 3:    14 ( 7.0%)  **************
 4:    15 ( 7.5%)  ***************
 5:    19 ( 9.5%)  *******************
 6:    24 (12.0%)  ************************
 7:    35 (17.5%)  ***********************************
 8:    24 (12.0%)  ************************
 9:    28 (14.0%)  ****************************
10:    18 ( 9.0%)  ******************
11:     9 ( 4.5%)  *********
12:     7 ( 3.5%)  *******

However, the error said: "ValueError: cannot switch from automatic field numbering to manual field specification" Any way I can get the code to have that output without the error?




Aucun commentaire:

Enregistrer un commentaire