r/ProgrammingLanguages • u/mttd • Jun 25 '26
LXM: Better Splittable Pseudorandom Number Generators (and Almost as Fast)
https://www.youtube.com/watch?v=XXh86oA-WOE
15
Upvotes
r/ProgrammingLanguages • u/mttd • Jun 25 '26
6
u/bl4nkSl8 Jun 25 '26
This is cool but should be on r/programming or something. It's not really anything about PL design or development
10
u/gasche Jun 25 '26 edited Jun 25 '26
We adopted this algorithm for the OCaml's standard library PRNG, and we are happy with this. (Technically it's a family of algorithms, we use L64X128.)
Splitting the PRNG state matters for some property-based testing applications, but for us it was a question of deterministic seeding on multicore programs: when you spawn a new thread ("domain" in OCaml parlance), we split the PRNG of the parent to decide the starting state of the child PRNG. This means that if you start your program by setting a fixed random seed, and then spawn various threads that draw random numbers independently from each other, the sequences of random numbers will (have good randomness and) not depend on the inverleaving/scheduling of the threads. This is a good property for reproducibility of random-using programs.
For a concrete example: if you fix a random seed, and then you start two threads
T1andT2, and they both generate a bunch of random numbers:If you had a single global PRNG state protected by a mutex, property (2) would not hold: if thread T1 runs while T2 is suspended, it will change the random values produced by T2 when it resumes. The user could also be careful to set a fixed (local) seed for each new thread on startup, but what seed do they use which preserves determinism and also good randomness?
(It is still possible to write programs where the actual random sequences depend on non-deterministic scheduling effects: for example you can have a data-race that makes a certain value non-deterministic, and then do a number of calls to the PRNG that depends on this non-deterministic value. But this is not typical/idiomatic PRNG usage, so most programs will benefit from this form of determinism anyway.)
2
u/david-1-1 Jun 26 '26
Nice to see Guy Steele still active, contributing a practical combined PRNG method along with lots of testing.