Advertisement
okpalan

custom-prng.py

Oct 3rd, 2023 (edited)
980
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. import hashlib
  2.  
  3. class CustomPRNG:
  4.     def __init__(self, seed=None):
  5.         if seed is None:
  6.             seed = self._generate_seed()
  7.         self.state = seed
  8.  
  9.     def _generate_seed(self):
  10.         return hashlib.sha256(str(hash(time.time())).encode()).digest()
  11.  
  12.     def _hash_state(self):
  13.         return hashlib.sha256(self.state).digest()
  14.  
  15.     def random(self):
  16.         # Use the current state as the seed for the next state
  17.         self.state = self._hash_state()
  18.        
  19.         # Convert the hashed state to a float in the range [0, 1)
  20.         return int.from_bytes(self.state, byteorder='big') / (2**256)
  21.  
  22. # Usage
  23. prng = CustomPRNG()
  24. for _ in range(10):
  25.     print(prng.random())
  26.  
Advertisement
Comments
  • okpalan
    211 days
    # text 0.22 KB | 0 0
    1. This is a custom-prng provides the foundation ready to be used for a prototype - testing chain commonly implemented using a linked list. Where a linkedlist contains the data from an initliaze seed. Note it is not complete
Add Comment
Please, Sign In to add comment
Advertisement