Making Python Less Random
Andrew Healey discussed debugging a Python game prototype with randomness bugs. He used ptrace to control system calls, ensuring consistent results for random functions without altering the code, enabling deterministic debugging.
Read original articleIn July 2024, Andrew Healey shared his experience of debugging a Python game prototype with tricky randomness bugs. He identified sources of randomness in his code and explored methods to achieve deterministic debugging without altering the code. By intercepting system calls using ptrace, he controlled the output of getrandom calls, ensuring consistent results for os.urandom and random.randint functions. Through a detailed explanation of the process, he demonstrated how to manipulate the Python process to return specific values, enabling deterministic outcomes for random number generation. This approach allowed for predictable results each time the Python process restarted, showcasing a method to handle randomness in Python programs without modifying the code directly. Andrew's exploration of system call tracing and manipulation provides insights into the inner workings of Python's randomness and offers a practical solution for deterministic debugging in Python applications.
Related
The Magic of Participatory Randomness
Randomness is vital in cryptography, gaming, and civic processes. Techniques like "Finger Dice" enable fair outcomes through participatory randomness, ensuring transparency and trust in provably fair games.
Exploring Randomness in JavaScript
This article compares Math.random() and Crypto.getRandomValues() in JavaScript for generating random values. Despite Crypto being more secure, Math.random() suffices for creating color palettes due to speed and perceived randomness.
The good, the bad, and the weird (2018)
Trail of Bits delves into "weird machines" in software exploitation, complex code snippets evading security measures. Techniques like Hoare triples and dynamic_casts aid in identifying and preventing exploitation, crucial in evolving security landscapes.
The weirdest QNX bug I've ever encountered
The author encountered a CPU usage bug in a QNX system's 'ps' utility due to a 15-year-old bug. Debugging revealed a race condition, leading to code modifications and a shift towards open-source solutions.
Making Python Less Random
Andrew Healey detailed his debugging journey with a Python game prototype facing randomness issues. He used ptrace to control system calls, ensuring consistent results without changing the code, showcasing advanced system call tracing techniques.
Trivial (and terrible) example – on every run past the first, the lists are identical:
#!/usr/bin/env python3
import pickle
import random
SEED = 42
if __name__ == "__main__":
r = random.Random(SEED)
rand_l = None
new_rand_l = []
try:
with open("rand_list.dat", "rb") as f:
rand_l = pickle.load(f)
except FileNotFoundError:
pass
for i in range(100):
new_rand_l.append(r.randint(0, 1000))
if rand_l is not None:
assert rand_l == new_rand_l
else:
with open("rand_list.dat", "wb") as f:
pickle.dump(new_rand_l, f)
[0]: https://docs.python.org/3/library/random.html#random.Random....Related
The Magic of Participatory Randomness
Randomness is vital in cryptography, gaming, and civic processes. Techniques like "Finger Dice" enable fair outcomes through participatory randomness, ensuring transparency and trust in provably fair games.
Exploring Randomness in JavaScript
This article compares Math.random() and Crypto.getRandomValues() in JavaScript for generating random values. Despite Crypto being more secure, Math.random() suffices for creating color palettes due to speed and perceived randomness.
The good, the bad, and the weird (2018)
Trail of Bits delves into "weird machines" in software exploitation, complex code snippets evading security measures. Techniques like Hoare triples and dynamic_casts aid in identifying and preventing exploitation, crucial in evolving security landscapes.
The weirdest QNX bug I've ever encountered
The author encountered a CPU usage bug in a QNX system's 'ps' utility due to a 15-year-old bug. Debugging revealed a race condition, leading to code modifications and a shift towards open-source solutions.
Making Python Less Random
Andrew Healey detailed his debugging journey with a Python game prototype facing randomness issues. He used ptrace to control system calls, ensuring consistent results without changing the code, showcasing advanced system call tracing techniques.