Skip to content

Instantly share code, notes, and snippets.

@innateessence
Created July 7, 2024 01:02
Show Gist options
  • Save innateessence/1ab5d4d5313138cc89c88b9891ae0176 to your computer and use it in GitHub Desktop.
Save innateessence/1ab5d4d5313138cc89c88b9891ae0176 to your computer and use it in GitHub Desktop.
Custom RNG
'''
Might want to port this to C/C++ for a microcontroller in the future
I'm sure the arduino IDE has a `rand()` package for a better, more optimized version though, but was still fun
'''
# Custom random number generator parameters
custom_seed = 123456789 # The seed value
custom_a = 1664525 # Multiplier
custom_c = 1013904223 # Increment
custom_m = 4294967296 # Modulus, 2^32
import time
# Custom random number generator function
def rand():
epoc_time = time.time()
global custom_seed
custom_seed += int(epoc_time * 1000)
custom_seed = (custom_a * custom_seed + custom_c) % custom_m
return custom_seed
def rand_range(start: int, end: int):
return start + rand() % (end - start)
if __name__ == "__main__":
results = []
for i in range(100):
res = rand_range(1, 1000)
results.append(res)
results.sort()
from pprint import pprint
pprint(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment