Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by azazeo ( 14 years ago )
#"The Chase" is a game played with two dice and an even number of players.

#The players sit around a table; the game begins with two opposite players having one die each. On each turn, the two players with a die roll it.
#If a player rolls a 1, he passes the die to his neighbour on the left; if he rolls a 6, he passes the die to his neighbour on the right; otherwise, he keeps the die for the next turn.
#The game ends when one player has both dice after they have been rolled and passed; that player has then lost.

#In a game with 100 players, what is the expected number of turns the game lasts?

#Give your answer rounded to ten significant digits.

import random

def dice():
 return random.randrange(1, 7)

def to_left(a):
 return((a-1)0)

def to_right(a):
 return((a+1)0)


a, b = 0, 50 #initial position

def game_step(a, b):
 d = dice()
 if d == 1:
  a = to_left(a)
 elif d == 6:
  a = to_right(a)

 d = dice()
 if d == 1:
  b = to_left(b)
 elif d == 6:
  b = to_right(b)

 return a, b

n = 0
def iteration(i, s):
 n, a, b = 0, 0, 50
 while a != b:
  a,b = game_step(a, b)
  n += 1
 s = s+n
 ev = float(s)/float(i)
 if i00 == 0:
  print("iter #{0:4d} steps_iter={1}\tsteps_tot={2:8d}  ev={3:.11f}".format(i,n,s,ev))
 return s, ev

total_iterations = 0
i, ev, delta = 1, 1, 1
ev_prev = 0


#!!!ACHTUNG - INSANE BRUTEFORCE!!!
while delta > 0.000001:
 total_iterations, ev = iteration(i, total_iterations)
 delta = abs(ev - ev_prev)
 ev_prev = ev
 if i00 == 0:
  print(delta)
 i += 1

 

Revise this Paste

Your Name: Code Language: