Skip to content

Instantly share code, notes, and snippets.

@greenmoss
Created March 14, 2023 15:59
Show Gist options
  • Save greenmoss/970805602b9d67949ae6af3a1b7ac50c to your computer and use it in GitHub Desktop.
Save greenmoss/970805602b9d67949ae6af3a1b7ac50c to your computer and use it in GitHub Desktop.
Exploration of fibonacci sequence as a source of random numbers
#!/usr/bin/env python3
import sys
import time
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def iterate_on_globals():
global iteration, current_number, previous_number
swapped_number = current_number
current_number += previous_number
previous_number = swapped_number
iteration += 1
def skip_iterations():
skip_iterations = 0
#skip_iterations = 10 * current_number
print(f"skipping past position {skip_iterations}")
for iteration in range(skip_iterations):
iterate_on_globals()
def print_statistics(table):
print(f"{bcolors.OKCYAN}{table}{bcolors.ENDC}")
table_total = 0
for number, count in table.items():
table_total += count
if table_total < 1: return
print(bcolors.OKGREEN, end='')
print(bcolors.UNDERLINE, end='')
for number, count in table.items():
percent = int(float(count) / float(table_total) * 100.)
print(number * percent, end='', sep=' ')
print(bcolors.ENDC)
def grow():
global iteration, current_number, previous_number
table = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0}
while True:
fake_random_digit = str(current_number % 10)
#fake_random_digit = str(current_number)[-2]
table[fake_random_digit] += 1
print(f"{bcolors.WARNING}iteration: {iteration: <5.3g} previous: {previous_number: <15.10g} current: {current_number: <15.10g} fake random digit: {fake_random_digit}{bcolors.ENDC}")
print_statistics(table)
iterate_on_globals()
time.sleep(0.1)
if __name__ == "__main__":
if (len(sys.argv) < 2):
print(f"usage: {sys.argv[0]} <starting integer>")
sys.exit(1)
seed = sys.argv[1]
print(f"seed is {seed}; press ctrl-c to stop")
previous_number = 1
current_number = int(seed)
iteration = 1
skip_iterations()
grow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment