Skip to content

Instantly share code, notes, and snippets.

@thenoviceoof
Created October 9, 2012 14:48
Show Gist options
  • Save thenoviceoof/3859288 to your computer and use it in GitHub Desktop.
Save thenoviceoof/3859288 to your computer and use it in GitHub Desktop.
Randomly generate automata
#!/usr/bin/env python
import os
import time
import random
rows, columns = os.popen('stty size', 'r').read().split()
columns = int(columns)
middle = columns/2
state = [0]*columns
state[middle] = 1
# state 30
n = {(0,0,0):0,
(0,0,1):1,
(0,1,0):1,
(0,1,1):1,
(1,0,0):1,
(1,0,1):0,
(1,1,0):0,
(1,1,1):0,
}
def rand_machine():
l = [(0,), (1,)]
for i in range(2):
l = [a+(0,) for a in l] + [a+(1,) for a in l]
d = dict()
for a in l:
d[a] = random.randint(0,1)
return d
while 1:
print "".join([{0:" ", 1:"#"}[c] for c in state])
next_state = [0]*columns
for i in range(columns-2):
next_state[i+1] = n[tuple(state[i:i+3])]
state = next_state
if random.random() < 0.4:
i = random.randint(0,columns-1)
state[i] = {0:1, 1:0}[state[i]]
if random.random() < 0.01:
n = rand_machine()
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment