Skip to content

Instantly share code, notes, and snippets.

@dobrosketchkun
Last active October 15, 2024 11:56
Show Gist options
  • Select an option

  • Save dobrosketchkun/99add8599482e086e6ecdc73433cd5cc to your computer and use it in GitHub Desktop.

Select an option

Save dobrosketchkun/99add8599482e086e6ecdc73433cd5cc to your computer and use it in GitHub Desktop.
One step above naïve implementation of pseudorandom number generator
import hashlib
class HashPseudoRandom:
"""
A class for generating a sequence of pseudorandom values using two hash functions.
"""
def __init__(self, seed, first_hash='sha3_512', second_hash='sha3_512'):
"""
Initialize the pseudorandom generator with the specified seed and hash functions.
Args:
seed: The initial state of the generator.
first_hash: The hash function to use for generating the current pseudorandom value.
second_hash: The hash function to use for generating the hidden state of the generator.
Raises:
ValueError: If either of the specified hash functions is not supported by the hashlib module.
"""
if first_hash not in hashlib.algorithms_available:
raise ValueError(f"{first_hash} is not a supported hash algorithm\nThe list of supported algorithms: \n{hashlib.algorithms_available}")
if second_hash not in hashlib.algorithms_available:
raise ValueError(f"{second_hash} is not a supported hash algorithm\nThe list of supported algorithms: \n{hashlib.algorithms_available}")
self.initial_seed = seed # Store the initial seed for resetting if needed
self.state = seed
self.first_hash = first_hash
self.second_hash = second_hash
self.count = 0
def generate_pseudorandom(self):
"""
Generate the next value in the sequence of pseudorandom values.
Returns:
The next pseudorandom value in the sequence.
"""
output = hashlib.new(self.first_hash, (str(self.count) + self.state).encode('utf-8')).hexdigest()
self.state = hashlib.new(self.second_hash, (str(self.count+1) + self.state).encode('utf-8')).hexdigest()
self.count += 1
return output
def reset(self, new_seed=None):
"""
Reset the pseudorandom generator to the initial or a new seed.
Args:
new_seed: The new seed to set. If None, it resets to the original seed.
"""
self.state = new_seed if new_seed else self.initial_seed
self.count = 0
def get_state(self):
"""
Get the current state of the generator.
Returns:
The current hidden state of the generator.
"""
return self.state
# Example usage
seed = "mysecretseed"
generator = HashPseudoRandom(seed)
# Generating pseudorandom values
for _ in range(5):
print(generator.generate_pseudorandom())
# Resetting the generator
generator.reset() # Resets to the original seed
# Generating pseudorandom values again after reset
for _ in range(3):
print(generator.generate_pseudorandom())
# Optionally, reset to a new seed
generator.reset(new_seed="newsecretseed")
# Generating pseudorandom values with a new seed
for _ in range(3):
print(generator.generate_pseudorandom())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment