Skip to content

Commit 0a72d14

Browse files
aeyjeyaryancclauss
andauthored
fix: inverted loop condition in chaos_machine.py prevents execution (#14777)
* 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 * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 7f81fcd commit 0a72d14

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

‎hashes/chaos_machine.py‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
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)
413
K = [0.33, 0.44, 0.55, 0.44, 0.33]
@@ -13,7 +22,15 @@
1322
machine_time = 0
1423

1524

16-
def push(seed) -> None:
25+
def push(seed: float) -> None:
26+
"""Push a seed value into the chaos machine.
27+
28+
Updates the internal buffer and parameter spaces using a logistic-map
29+
transition function.
30+
31+
Args:
32+
seed: A numeric seed to push into the machine.
33+
"""
1734
global buffer_space, params_space, machine_time
1835

1936
# Choosing Dynamical Systems (All)
@@ -39,9 +56,29 @@ def push(seed) -> None:
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
4474

75+
# PRNG (Xorshift by George Marsaglia)
76+
def xorshift(x: int, y: int) -> int:
77+
x ^= y >> 13
78+
y ^= x << 17
79+
x ^= y >> 5
80+
return x
81+
4582
# Choosing Dynamical Systems (Increment)
4683
key = machine_time % m
4784

@@ -70,9 +107,10 @@ def pull():
70107

71108

72109
def reset() -> None:
110+
"""Reset the chaos machine to its initial state."""
73111
global buffer_space, params_space, machine_time
74112

75-
buffer_space = K
113+
buffer_space = K.copy()
76114
params_space = [0] * m
77115
machine_time = 0
78116

0 commit comments

Comments
 (0)