jeudi 11 juillet 2019

Pitfalls of RNG tehcniques

I can use one of two methods to create a pseudo random number sequence that has two important characteristics - (1) it is reproducible on different machines, and (2) the sequence never repeats a number within range until all have been emitted.

My question is - do either of these methods have potential issues with regards to portability?

Note that they don't produce the same ranges as each other, but that's ok for my purposes. The numbers just have to appear random to the human eye.

Method 1
generates a random sample from a range using native Python library functions. It's slow if I use large ranges (10m or more) but OK for relatively small ones, and is much easier to understand for people without a degree in maths :

import random
random.seed(5)
x = random.sample(range(10000,99999),89999)
for i in range(10):
   print(x[i])

Method 2
uses an algorithm not from a Python library :
(https://en.wikipedia.org/wiki/Linear_congruential_generator)
It's super fast even for massive ranges, but harder to understand and therefore spot potential issues with :

def lcg(modulus, a, c, seed):
  while True:
    seed = (a * seed + c) % modulus
    yield seed


m = 10000019
c = int(m/2)
a = 5653
s = a

g = lcg(m,a,c,s)
for _ in range(10):
  print(next(g))

Note I am more than open to alternatives :)




Aucun commentaire:

Enregistrer un commentaire