import pylab import random def simdices(): values = [1,2,3,4,5,6, 7,8,9,10] results = { 1:0 , 2:0 , 3:0 , 4:0 , 5:0 , 6:0, 7:0, 8:0, 9:0, 10:0 } results2 = { 1:0 , 2:0 , 3:0 , 4:0 , 5:0 , 6:0, 7:0, 8:0, 9:0, 10:0 } resultsCombined = { 1:0 , 2:0 , 3:0 , 4:0 , 5:0 , 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 14:0, 15:0, 16:0, 17:0, 18:0, 19:0, 20:0 } for i in range(1000000): value1 = random.choice(values) value2 = random.choice(values) results[value1] +=1 results2[value2] +=1 resultsCombined[value1+value2] +=1 print results.values() bins = [1,2,3,4,5,6, 7,8,9,10,11,12,13,14,15,16,17,18,19,20] #y = results.values() #pylab.hist(results.values(), histtype='bar', color='r', stacked=True, align = 'mid', alpha=0.5, label='Vowels proportion') pylab.bar(values, results.values(), align='center', color='r', alpha=0.2, label='Count of results') pylab.bar(values, results2.values(), align='center', color='b', alpha=0.5, label='Count of results 2') pylab.bar(bins, resultsCombined.values(), align='center', color='g', alpha=0.5, label='Count of results combined') pylab.title("Results for 1 million rolling of a die with 10 faces") pylab.xlim([1,20]) pylab.xlabel("Face result") pylab.ylabel("Frequency") pylab.xticks(bins) #pylab.fill_between(x, y1, y2, where=y2>=y1, facecolor='green', interpolate=True) pylab.grid(True) pylab.legend() #pylab.savefig('L15_P5_graph.png') pylab.show() simdices()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question