def main(argv): M = int(argv[1]) N = int(argv[2]) S = int(argv[3]) # Create a RandomQueue object containing Queue objects. servers = randomqueue.RandomQueue() for i in range(M): servers.enqueue(linkedlistqueue.Queue()) for j in range(N): # Assign an item to a server. min = servers.sample() for k in range(1, S): # Pick a random server, update if new min. q = servers.sample() if len(q) < len(min): min = q # min is the shortest server queue. min.enqueue(j) lengths = [] for q in servers: lengths += [len(q)] stddraw.createWindow() stddraw.setYscale(0, 2.0*N/M) stdstats.plotBars(lengths) stddraw.show() stddraw.wait()
def main(argv): M = int(argv[1]) N = int(argv[2]) S = int(argv[3]) # Create a RandomQueue object containing Queue objects. servers = randomqueue.RandomQueue() for i in range(M): servers.enqueue(linkedlistqueue.Queue()) for j in range(N): # Assign an item to a server. min = servers.sample() for k in range(1, S): # Pick a random server, update if new min. q = servers.sample() if len(q) < len(min): min = q # min is the shortest server queue. min.enqueue(j) lengths = [] for q in servers: lengths += [len(q)] stddraw.createWindow() stddraw.setYscale(0, 2.0 * N / M) stdstats.plotBars(lengths) stddraw.show() stddraw.wait()
def main(): #print(deck()) a = deck() #b = popdeck(a,5) b = [16,2,4,1,0] r = pair(b) #获得对数或三条数 num = numofGroupDeck(b) #获得牌面数字 c = colorofGroupDeck(b) #获得花色 print('牌:',b) print('牌面数字:',num) print('花色:',c) print('一对:',onePair(r)) print('两对:',twoPair(r)) print('三条:',threeofAkind(r)) print('满堂红:',fullHouse(r)) print('同花:',flushColor(c)) print('顺子:',straight(num)) print('同花顺:',straightFlush(c,num)) #统计各种情况出现概率 n = 100000 #试验次数 p = [0]*7 #记录各种情况结果 for i in range(n): a = deck() b = popdeck(a,5) r = pair(b) num = numofGroupDeck(b) c = colorofGroupDeck(b) if onePair(r): p[0] += 1 elif twoPair(r): p[1] += 1 elif threeofAkind(r): p[2] += 1 elif fullHouse(r): p[3] += 1 if straightFlush(c,num): p[6] += 1 elif flushColor(c): p[4] += 1 elif straight(num): p[5] += 1 print('p:',p) stddraw.setYscale(0,1.1*max(p)) stdstats.plotBars(p) #使用数学公式验证,先选牌面数字,再选花色 q = [0]*7 q[0] = comb(13,1)*comb(4,2)*comb(12,3)*comb(4,1)**3/(comb(52,5)) q[1] = comb(13,2)*comb(4,2)**2*comb(11,1)*comb(4,1)/(comb(52,5)) q[2] = comb(13,1)*comb(4,3)*comb(12,2)*comb(4,1)**2/(comb(52,5)) q[3] = comb(13,1)*comb(4,3)*comb(12,1)*comb(4,2)/(comb(52,5)) flush = comb(13,5)*comb(4,1)/(comb(52,5)) Straight = 9*comb(4,1)**5/(comb(52,5)) q[6] = flush*Straight q[4] = flush-q[6] q[5] = Straight-q[6] for i in range(len(q)): print('q[i]:{:.4f}'.format(q[i])) stddraw.show()
def main(): n = int(sys.argv[1]) p = float(sys.argv[2]) trials = int(sys.argv[3]) t = int(sys.argv[4]) q = evaluate(n, p, trials) stdio.writeln(q) norm = exTimes(n, p, trials, t) phi = stdarray.create1D(n + 1, 0.0) stddev = math.sqrt(n) / 2.0 for i in range(n + 1): phi[i] = gaussian.pdf(i, n / 2.0, stddev) stddraw.setCanvasSize(1000, 400) stddraw.setYscale(0, 1.1 * max(max(norm), max(phi))) stdstats.plotBars(norm) stdstats.plotLines(phi) stddraw.show()
def main(argv): n = int(argv[1]) t = int(argv[2]) stddraw.createWindow() stddraw.setYscale(0, 0.2) freq = [0] * (n+1) for t in range(t): freq[binomial(n)] += 1 norm = [0.0] * (n+1) for i in range(n+1): norm[i] = float(freq[i]) / float(t) stdstats.plotBars(norm) stddev = math.sqrt(n) / 2.0 phi = [0.0] * (n+1) for i in range(n+1): phi[i] = gaussian.phi(i, n/2.0, stddev) stdstats.plotLines(phi) stddraw.show() stddraw.wait()
def draw(self): stddraw.setYscale(0, self._max) stdstats.plotBars(self._freq)
import stdstats import stddraw #a = [1,1,9,3,5,6,8,10] a = [1] print('mean:', stdstats.mean(a)) print('var:', stdstats.var(a)) print('stddev:', stdstats.stddev(a)) print('median:', stdstats.median(a)) stddraw.setYscale(min(a) - 2, max(a) + 2) stdstats.plotPoints(a) stdstats.plotLines(a) stdstats.plotBars(a) stddraw.show()
# draw the results to standard draw. Also draw the predicted Gaussian # distribution function, thereby allowing easy comparison of the # experimental results to the theoretically predicted results. n = int(sys.argv[1]) trials = int(sys.argv[2]) freq = stdarray.create1D(n + 1, 0) for t in range(trials): heads = stdrandom.binomial(n, 0.5) freq[heads] += 1 norm = stdarray.create1D(n + 1, 0.0) for i in range(n + 1): norm[i] = 1.0 * freq[i] / trials phi = stdarray.create1D(n + 1, 0.0) stddev = math.sqrt(n) / 2.0 for i in range(n + 1): phi[i] = gaussian.pdf(i, n / 2.0, stddev) stddraw.setCanvasSize(1000, 400) stddraw.setYscale(0, 1.1 * max(max(norm), max(phi))) stdstats.plotBars(norm) stdstats.plotLines(phi) stddraw.show() #----------------------------------------------------------------------- # python bernoulli.py 20 100000
def drawDice(a=[]): stddraw.setYscale(0, 1.1 * max(a)) stdstats.plotBars(a)
# Create a RandomQueue object containing Queue objects. servers = RandomQueue() for i in range(serverCount): servers.enqueue(Queue()) for j in range(itemCount): # Assign an item to a server. best = servers.sample() for k in range(1, sampleSize): # Pick a random server, update if new best. queue = servers.sample() if len(queue) < len(best): best = queue # best is the shortest server queue. best.enqueue(j) lengths = [] while not servers.isEmpty(): lengths += [len(servers.dequeue())] stddraw.clear(stddraw.LIGHT_GRAY) stddraw.setYscale(0, 2.0*itemCount/serverCount) stdstats.plotBars(lengths) stddraw.show() #----------------------------------------------------------------------- # python loadbalance.py 50 500 1 # python loadbalance.py 50 500 2
# Create a RandomQueue object containing Queue objects. servers = RandomQueue() for i in range(serverCount): servers.enqueue(Queue()) for j in range(itemCount): # Assign an item to a server. best = servers.sample() for k in range(1, sampleSize): # Pick a random server, update if new best. queue = servers.sample() if len(queue) < len(best): best = queue # best is the shortest server queue. best.enqueue(j) lengths = [] while not servers.isEmpty(): lengths += [len(servers.dequeue())] stddraw.clear(stddraw.LIGHT_GRAY) stddraw.setYscale(0, 2.0 * itemCount / serverCount) stdstats.plotBars(lengths) stddraw.show() # ----------------------------------------------------------------------- # python loadbalance.py 50 500 1 # python loadbalance.py 50 500 2
def draw(self): stddraw.setYscale(0, max(self._freqCounts)) stdstats.plotBars(self._freqCounts)
# distribution function, thereby allowing easy comparison of the # experimental results to the theoretically predicted results. n = int(sys.argv[1]) trials = int(sys.argv[2]) freq = stdarray.create1D(n+1, 0) for t in range(trials): heads = stdrandom.binomial(n, 0.5) freq[heads] += 1 norm = stdarray.create1D(n+1, 0.0) for i in range(n+1): norm[i] = 1.0 * freq[i] / trials phi = stdarray.create1D(n+1, 0.0) stddev = math.sqrt(n)/2.0 for i in range(n+1): phi[i] = gaussian.pdf(i, n/2.0, stddev) stddraw.setCanvasSize(1000, 400) stddraw.setYscale(0, 1.1 * max(max(norm), max(phi))) stdstats.plotBars(norm) stdstats.plotLines(phi) stddraw.show() #----------------------------------------------------------------------- # python bernoulli.py 20 100000