Skip to content

Instantly share code, notes, and snippets.

@natbusa
Last active December 11, 2015 01:58
Show Gist options
  • Select an option

  • Save natbusa/4527007 to your computer and use it in GitHub Desktop.

Select an option

Save natbusa/4527007 to your computer and use it in GitHub Desktop.
The game of the goose and markov chains.
# Natalino Busa
# twitter.com/natalinobusa
# linkedin.com/in/natalinobusa
# www.natalinobusa.com
import numpy as np
from matplotlib import pyplot
def special(p, pos_start, pos_end):
p[pos_start]= np.zeros(64)
p[pos_start][pos_start]=1
for i in range(64):
pp = p[i][pos_start]
p[i][pos_start] = 0
p[i][pos_end] = p[i][pos_end] + pp
def snake(p, pos_start, pos_end=0):
if (pos_end<pos_start):
special(p, pos_start, pos_end)
def ladder(p, pos_start, pos_end=63):
if (pos_end>pos_start):
special(p, pos_start, pos_end)
def fairgame():
p=np.zeros((64,64))
for i in range(64):
for j in range(6):
if (i+j+1<64):
p[i][i+j+1]=1.0/6.0
p[63][63]=1
p[62][63]=6.0/6.0
p[61][63]=5.0/6.0
p[60][63]=4.0/6.0
p[59][63]=3.0/6.0
p[58][63]=2.0/6.0
p[57][63]=1.0/6.0
return p
a=np.zeros(64)
m=fairgame()
snake(m,15)
snake(m,10,8)
for k in range(63):
pyplot.figure(1)
pyplot.plot(m[0][0:62])
pyplot.figure(2)
pyplot.plot(k, m[0][63], 'b.', markersize=5)
a=a+m[0]
pyplot.figure(3)
pyplot.plot(a[0:62])
m=np.dot(m,p)
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment