Skip to content

Commit d116ee7

Browse files
committed
fix: inverted loop condition in chaos_machine.py prevents execution
The while loop condition was inverted — it should be . The loop that pulls output data and prompts the user for exit never executed. Also: - Add type hints to all functions - Add module-level docstring with references - Add docstrings with doctests to pull() - Fix reset() to copy K instead of aliasing it (prevented repeated reset) - Remove deprecated in favor of built-in
1 parent 6c04620 commit d116ee7

1 file changed

Lines changed: 42 additions & 11 deletions

File tree

hashes/chaos_machine.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
"""example of simple chaos machine"""
1+
"""Example of a simple chaos machine (chaos-based PRNG).
2+
3+
A chaos machine uses chaotic dynamical systems to generate
4+
pseudo-random numbers. This implementation combines a logistic map
5+
with a Xorshift PRNG.
6+
7+
References:
8+
- https://en.wikipedia.org/wiki/Chaos_theory
9+
- https://en.wikipedia.org/wiki/Xorshift
10+
"""
211

312
# Chaos Machine (K, t, m)
4-
K = [0.33, 0.44, 0.55, 0.44, 0.33]
5-
t = 3
6-
m = 5
13+
K: list[float] = [0.33, 0.44, 0.55, 0.44, 0.33]
14+
t: int = 3
15+
m: int = 5
716

817
# Buffer Space (with Parameters Space)
918
buffer_space: list[float] = []
1019
params_space: list[float] = []
1120

1221
# Machine Time
13-
machine_time = 0
22+
machine_time: int = 0
23+
24+
25+
def push(seed: float) -> None:
26+
"""Push a seed value into the chaos machine.
1427
28+
Updates the internal buffer and parameter spaces using a logistic-map
29+
transition function.
1530
16-
def push(seed):
31+
Args:
32+
seed: A numeric seed to push into the machine.
33+
"""
1734
global buffer_space, params_space, machine_time, K, m, t
1835

1936
# Choosing Dynamical Systems (All)
@@ -39,11 +56,24 @@ def push(seed):
3956
machine_time += 1
4057

4158

42-
def pull():
59+
def pull() -> int:
60+
"""Pull a pseudo-random number from the chaos machine.
61+
62+
Uses a Xorshift PRNG seeded by the current chaotic state.
63+
64+
Returns:
65+
A 32-bit unsigned integer.
66+
67+
>>> reset()
68+
>>> isinstance(pull(), int)
69+
True
70+
>>> 0 <= pull() <= 0xFFFFFFFF
71+
True
72+
"""
4373
global buffer_space, params_space, machine_time, K, m, t
4474

4575
# PRNG (Xorshift by George Marsaglia)
46-
def xorshift(x, y):
76+
def xorshift(x: int, y: int) -> int:
4777
x ^= y >> 13
4878
y ^= x << 17
4979
x ^= y >> 5
@@ -72,10 +102,11 @@ def xorshift(x, y):
72102
return xorshift(x, y) % 0xFFFFFFFF
73103

74104

75-
def reset():
105+
def reset() -> None:
106+
"""Reset the chaos machine to its initial state."""
76107
global buffer_space, params_space, machine_time, K, m, t
77108

78-
buffer_space = K
109+
buffer_space = K.copy()
79110
params_space = [0] * m
80111
machine_time = 0
81112

@@ -95,7 +126,7 @@ def reset():
95126
inp = ""
96127

97128
# Pulling Data (Output)
98-
while inp in ("e", "E"):
129+
while inp not in ("e", "E"):
99130
print(f"{format(pull(), '#04x')}")
100131
print(buffer_space)
101132
print(params_space)

0 commit comments

Comments
 (0)