Skip to content

Instantly share code, notes, and snippets.

@riceluxs1t
Created April 25, 2016 16:06
Show Gist options
  • Save riceluxs1t/671382af1bfd17a97d4588c2438e1994 to your computer and use it in GitHub Desktop.
Save riceluxs1t/671382af1bfd17a97d4588c2438e1994 to your computer and use it in GitHub Desktop.
import random
def mixUp(x, y):
child_x = random_child(x)
child_y = random_child(y)
num_rows =len(x)
num_cols = len(x[0])
new_child = []
for i in range(num_rows):
new_child.append([0]*num_cols)
for i in range(num_rows):
for j in range(num_cols):
new_child[i][j] = (child_x[i][j], child_y[i][j])
is_pure_blood = findOut(new_child)
return is_pure_blood, new_child
def random_child(x):
new_child = []
num_rows =len(x)
num_cols = len(x[0])
for i in range(num_rows):
new_child.append([0]*num_cols)
for i in range(num_rows):
for j in range(num_cols):
coin_flip = random.random()
if coin_flip < 0.5:
new_child[i][j] = x[i][j][0]
else:
new_child[i][j] = x[i][j][1]
return new_child
def findOut(candidate):
num_rows =len(candidate)
num_cols = len(candidate[0])
for i in range(num_rows):
for j in range(num_cols):
if candidate[i][j][0] != candidate[i][j][1]:
return False
return True
def simulate(x, y, num_simulations):
global_ans = 0
for _ in range(num_simulations):
ans = 0
while True:
ans += 1
is_pure_blood_one, new_child_one = mixUp(x, y)
is_pure_blood_two, new_child_two = mixUp(x, y)
if is_pure_blood_one:
print 'breaking after {0} generation, with x = {1}, y = {2}, child = {3}'.format(ans, x, y, new_child_one)
break
x = new_child_one
y = new_child_two
global_ans += ans
return global_ans / float(num_simulations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment