Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import random
class tfgol:
""" A 2-D 64 cell finite cellular automaton (Game of Life)"""
def __init__(self, seed, n=8):
self.seed = seed
self.n = n
self.prev = [[0]*n for i in range(n)]
self.cur = [[0]*n for i in range(n)]
self.bootstrap(seed)
def bootstrap(self,seed):
grid = [[0]*self.n for i in range(self.n)]
for i in range(self.n):
for j in range(self.n):
try:
shift = (self.n*i) + j
x = (seed>>shift) & 1
grid[i][j] = x
except:
grid[i][j] = 0
self.prev = grid
self.nextgen()
return
def nextgen(self):
for i in range(self.n):
for j in range(self.n):
s = self.prev[i%8][(j-1)%self.n]+ \
self.prev[i%self.n][(j+1)%self.n]+ \
self.prev[(i+1)%self.n][(j)%self.n]+ \
self.prev[(i-1)%self.n][(j)%self.n]+ \
self.prev[(i-1)%self.n][(j-1)%self.n]+ \
self.prev[(i-1)%self.n][(j+1)%self.n]+ \
self.prev[(i+1)%self.n][(j+1)%self.n]+ \
self.prev[(i+1)%self.n][(j-1)%self.n]
if self.prev[i][j] == 1 and (s == 2 or s == 3):
self.cur[i][j] = 1
elif self.prev[i][j] == 0 and s == 3:
self.cur[i][j] = 1
else:
self.cur[i][j] = 0
self.prev=self.cur
return
def __str__(self):
b =""
for i in range(self.n):
b += "".join(str(c) for c in self.cur[i])
d = int(b, base=2)
d = str(d)
# return b + "\t" + d
return d
f = open("tfgol_data.csv","a")
f.write("seed,msb,lsb,hw,reps,cv\n")
for t in range(10000):
seed = random.randint(1,2**64)
msb = (seed >> 63) & 1
lsb = seed & 1
hw = bin(seed).count("1")
ca = tfgol(seed)
last = ""
reps = 0
for g in range(1,10001):
v = str(ca)
if v == last:
reps = g
cv = v
break
last = v
ca.nextgen()
if g == 10000:
reps = -1
cv = -1
f.write(f"{seed},{msb},{lsb},{hw},{reps},{cv}\n")
f.close()