Skip to content

Commit

Permalink
add pseudo-random number generation example
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhus22 committed Jun 6, 2024
1 parent e54ab77 commit 0d085fd
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/pseudo-random/prandom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pyaccell import Automata
from pyaccell import create_rule
from pyaccell import vec_to_list
from sys import argv
from math import log2

def transition(state, neighbour):
if (state == 1) and neighbour[1] < 2:
return 0
if (state == 1) and neighbour[1] > 3:
return 0
if (state == 0) and neighbour[1] == 3:
return 1

return state


def random(max):
states = 2
rule = create_rule(transition, states)
ca = Automata(rule, states)
if (max > ca.sim_width * ca.sim_height):
ca = Automata(rule, states, max / 1024, 1024)
ca.run(3)
output = vec_to_list(ca.output)
binary = ''.join(map(str, output))
num = int(binary, 2)
return (num % max)

if __name__ == '__main__':
if len(argv) != 2:
print("Usage: main.py <maximum value>")
exit(1)
_max = int(argv[1])
n = random(_max)
print(n)

0 comments on commit 0d085fd

Please sign in to comment.