This article to share the content is about Python knowledge of the dice game, has a certain reference value, the need for friends can refer to. Recently learned some statistics and python knowledge, try to analyze the dice game. Dice according to standard 6-sided, analysis of a cast 1, 2, 3, 4, throwing 10, 100, 1000, 10,000 times the results.
Using tools
Jupyter Notebook Analysis Tool
Matplotlib, Pygal visualization Package
1. Number of guesses
Number of occurrences of each number in 1-6
# Importing Package import Pygalimport NumPy as Npimport matplotlib.pyplot as pltplt.rcparams[' Font.sans-serif ']=[' Simhei '] # Used to display the Chinese label normally plt.rcparams[' Axes.unicode_minus ']=false # used to display the negative sign normally from the random import Randint
# Use random data to simulate roll dice # each time a number of 1-6 is displayed num_sides = 6 # dice 6 faces def getData (n, Times): "" " defines the function, gets the throw data N: means to throw a few dice at a time C4/>times: Indicates a total of several "" " results = [] for N in range (1,n+1): A for roll_num in range: result = Randint (1,num_sides) results.append (Result) return results
# Print Throw result print (GetData (1,10) # 1 dice Roll 10 times print (GetData (2,5)) # 2 dice throw 5 times
[2, 2, 2, 2, 1, 6, 4, 4, 5, 5] [4, 3, 5, 6, 2, 2, 3, 6, 4, 4]
# Analysis Results # count the number of occurrences of each number and display the picture # N: means to throw a number of dice at once # data represents the throwing data def showresult (N, times): frequencies = [] for Valu E in range (1, num_sides+1): frequency = GetData (N, Times). Count (value) frequencies.append (frequency) # Data Visualization # this time using Pygal to generate SVG format vector graph hist = Pygal. Bar () hist.title = str (N) + "dice throw" + str (times) + "second result" hist.x_labels = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 '] hist.x_ title = "Points" hist.y_title = "occurrences" hist.add (str (N) + ' dice ', frequencies) Hist.render_to_file (' 1-' +str (N ) +str (times) + '. svg ') # stored as a vector image
# One dice throw 10,100, 1000, 10,000 results Analysis Showresult (1,10) showresult (1,100) Showresult (1,1000) Showresult (1,10000)
# 2 Dice Toss 10,100, 1000, 10,000 times result Analysis Showresult (2,10) showresult (2,100) showresult (2,1000) Showresult (2,10000)
3 Dice, 4 dice is no longer.
We find that the more times we throw, the more likely the probability of each number to appear, and the closer we tend to the same.
2, guess the size
Each throw point and
# Each throw point and Def getData2 (N, Times): "" " defines the function, gets the throw data N: Represents a time with several dice to cast the Times: The total number of cast" "" results = [ ] for roll_num in range: result = 0 for n in Range (1,n+1): result + = Randint (1,num_sides) Results.append (Result) return results
# Print Throw result print (GetData2 (1,10) # 1 dice Roll 10 times print (GETDATA2 (2,5)) # 2 dice throw 5 times
[4, 3, 6, 2, 5, 4, 5, 3, 6, 2] [6, 10, 5, 8, 7]
# Analysis Results # Statistics and number of occurrences and display picture # N: Indicates one time with a few dice to cast # data represents the throwing of the date def showResult2 (N, times): frequencies = [] for Valu E in range (n, n*num_sides+1): frequency = GETDATA2 (n, Times). Count (value) frequencies.append (frequency) # Data Visualization # this time using matplotlib to generate pictures X_num = n*num_sides+1-n idx = Np.arange (x_num) width = 0.5 sn = str (N sm = str (times) x_labels = [STR (n) for n in range (n, n*num_sides+1)] # X-axis scale Plt.bar (idx, Frequencie s, width, color= ' red ', label=sn+ ' dice ') Plt.xlabel (' pips and ') Plt.ylabel (' Number of occurrences ') plt.title (sn+ ' dice throw ' + SM + ' results ') plt.xticks (idx, x_labels) plt.legend () # show Legend Plt.show ()
1 Dice guess the size does not make much sense, we directly to analyze the situation of two dice.
# 2 Dice Toss 10,100, 1000, 10,000 times result Analysis ShowResult2 (2,10) showResult2 (2,100) showResult2 (2,1000) showResult2 (2,10000)
# 3 Dice Toss 10,100, 1000, 10,000 times result Analysis ShowResult2 (3,10) showResult2 (3,100) showResult2 (3,1000) showResult2 (3,10000)
# 4 Dice Toss 10,100, 1000, 10,000 times result Analysis ShowResult2 (4,10) showResult2 (4,100) showResult2 (4,1000) showResult2 (4,10000)
From the above pictures we can see that when the number of throws is long enough, the probability of the occurrence of large/small points is basically the same, the point size is normal distribution characteristics.
Related recommendations:
Dice Game-C language implementation
Dice Game
C + + Roll dice game
Practiced hand small program: Craps Dice Game
Roll the dice problem