• API
• FAQ
• Tools
• Archive
daily pastebin goal
13%
SHARE
TWEET

# Untitled

a guest Apr 17th, 2018 52 Never
ENDING IN00days00hours00mins00secs

1.
2. # 12:58 PM 12/1/2011
3. # based on playing Portal2, trying to figure out the typical
4. # number of tries to win 3 rock-paper-scissors in a row by
5. # a Monte Carlo simulation.
6.
7. import random
8.
9. MAX_SIMULATION_RUNS = 1000
10. WINS_REQUIRED = 3
11.
12. def Random(N):
13.     return random.randrange(N)
14.
15. handtype = ( 'R', 'P', 'S',)
16. resulttype = ( '>', '=', '<',)
17.
18. # 0 - A wins, 1 - tie, 2 - B wins
19. def calcresult(a,b):
20.     return ((b - a) + 4) % 3
21.
22. def buildresultstring(a,b):
23.     s = "%c" % handtype[a]
24.     s = s + "%c" % resulttype[calcresult(a,b)]
25.     s = s + "%c" % handtype[b]
26.     return s
27.
28. totalplaycount = 0
29.
30. gamelenhistory = []
31.
32. for simulation_run in range(MAX_SIMULATION_RUNS):
33.     print "Simulation number %u:" % simulation_run
34.     winner = 0
35.     playcount = 0
36.     while winner < WINS_REQUIRED:
37.         playcount +=1
38.         a = Random(3)
39.         b = Random(3)
40.         print buildresultstring(a,b),
41.         if calcresult(a,b) == 0:
42.             winner += 1
43.         else:
44.             winner = 0
45.     print "WIN in %u!" % playcount
46.     totalplaycount += playcount
47.     gamelenhistory.append(playcount)
48.
49. print "Average of %0.1f tries to win %u in a row." % (
50.             float(totalplaycount) / MAX_SIMULATION_RUNS,
51.             WINS_REQUIRED)
52.
53. gamelenhistory = sorted( gamelenhistory)
54.
55. print "Median is about %u tries." % ( gamelenhistory[
56.             len(gamelenhistory) >> 1])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top