Jump to content

One-dimensional cellular automata: Difference between revisions

→‎{{header|Python}}: Correct for fixed end values, variable length sequence
(→‎{{header|Python}}: Boolean version)
(→‎{{header|Python}}: Correct for fixed end values, variable length sequence)
Line 243:
<python>import random
 
nquads = 5
maxgenerations = 10
fmt = '%%0%ix'%nquads
#a = int('01110110101010100100', 2)
nbits = 4*nquads
a = random.getrandbits(20nbits) << 1
#a = int('01110110101010100100', 2) << 1
endmask = (2<<nbits)-2;
endvals = 0<<(nbits+1) | 0
tr = ('____', '___#', '__#_', '__##', '_#__', '_#_#', '_##_', '_###',
'#___', '#__#', '#_#_', '#_##', '##__', '##_#', '###_', '####' )
for i in range(maxgenerations):
print "Generation %3i: %s" % (i,(''.join(tr[int(t,16)] for t in ('%05x'fmt%(a>>1)))))
a |= endvals
a = ((a & ((a<<1) | (a>>1))) ^ ((a<<1) & (a>>1))) & endmask</python>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.