lundi 5 décembre 2016

Canonical way to generate random numbers in Cython

What is the best way to generate uniform random numbers (e.g a double in [0, 1)) that is:

  1. Cross platform (ideally with same same sample sequence)
  2. Thread safe (explicit passing of the mutated state of the prng or using a thread-local state internally)
  3. No GIL lock
  4. Easily wrappable in Cython

There was a similar post over 3 years ago about this but a lot of the answers don't meet all criteria. For example, drand48 is POSIX-specific.

The only method I'm aware of that seems (but not sure) to meet all criteria is:

from libc.stdlib cimport rand, RAND_MAX

random = rand() / (RAND_MAX + 1.0)

Note @ogrisel asked the same question about 3 years ago.




Aucun commentaire:

Enregistrer un commentaire