Emre Alpogunc
Emre Alpogunc

Reputation: 51

Rolling Dice Histogram Not Giving The Supposed Output?

So here's my code: You can see the code down below:

import random
from collections import defaultdict

def main():
    dice = 11
    sides = 6
    rolls = 1000
    result = roll(dice, sides, rolls)
    maxH = 0
    for i in range(dice, dice * sides + 1):
        if result[i] / rolls > maxH: maxH = result[i] / rolls
    for i in range(dice, dice * sides + 1):
        print('{:2d}{:10d} {}'.format(i, result[i], '*' * int(result[i] / rolls / maxH * 40)))


def roll(dice, sides, rolls):
    d = defaultdict(int)
    for _ in range(rolls):
        d[sum(random.randint(1, sides) for _ in range(dice))] += 1
    return d

main()

And the output that I want is:

11:
12:
13:
14:
15:
16:
17:
18: *
19: ****
20:
21: ***
22: ******
23: ********
24: ****************
25: *************
26: **********
27: *********************************
28: ****************************************
29: *********************************
30: ***************************************************
31: *****************************************************************
32: ********************************************************
33: **************************************************************************************
34: ***********************************************************
35: *********************************************************************
36: ***********************************************************************************
37: **************************************************************
38: *****************************************************************
39: ***************************************
40: *****************************************************
41: ************************************ 
42: ****************************
43: ************************
44: ************************
45: *********
46: ***********
47: *******
48: ***
49: **
50:
51:
52: *
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66: 

But for some reason it's not giving me this exact output.What's the problem, can't find a solution ? (Note: The question of this code was this: "Write a program that takes an integer argument n, and rolls 11 fair six-sided dice, n times. Use an integer array to tabulate the number of times each possible total (between 11 and 66) occurs. Then print a text histogram of the results, as illustrated below. (You can take n as 1000).")

Upvotes: 1

Views: 350

Answers (1)

mohit kaushik
mohit kaushik

Reputation: 26

print('{:2d}: {}'.format(i, '*' * int(result[i] / rolls / maxH * 80)))

maxH = max(result.values()) 

and then do

print('{:2d}: {}'.format(i, '*' * int(result[i] / maxH * 100)))

Upvotes: 1

Related Questions