Should we upgrade to a new PRNG in core?
[email protected] (Scott Baker)
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <[email protected]> |
Esteemed p5p:
Almost two years ago I brought up
<https://www.nntp.perl.org/group/perl.perl5.porters/2024/11/msg269037.html>
upgrading *rand()* in core to use a more modern PRNG. There was much
lively discussion and many opinions were shared. Ultimately it resulted
in me writing Random::Simple <https://metacpan.org/pod/Random::Simple>
as a drop in replacement to upgrade *rand()* and *srand()*. At the time
there were questions about whether we could or should upgrade the PRNG.
After much hacking and learning Perl core I have a working PR
<https://github.com/Perl/perl5/pull/24105> that proves it's actually
pretty easy to upgrade the PRNG. Whoever designed things back in the day
made the PRNG configurable in Configure, so really all it took was some
new functions and to point Configure at them instead of drand48(). This
PR includes two PRNGs as options to show how simple it is to switch
between them using Configure.
This PR *is not merge ready* yet, it's more proof-of-concept that we
*could *upgrade the PRNG without any major breakage.
Ultimately the question becomes: Knowing the limitations of drand48() do
we want to upgrade the PRNG in core? Or is it "good enough" and users
that want something better are free to use CPAN.
Completed items
* Modern PRNG implementation (PCG64)
* Detailed instructions for future devs on how to change/upgrade the PRNG
* Updated unit tests
* Verify |srand()| functionality works as expected
* Verify the new |rand()| outputs the full 53 bit state capable from a
double (drand48 could only do 48 bits)
o |./perl -I lib -E 'for (1..5) { printf("%064b\n", rand() *
2**64-1); }'|
TODO
* |prng.h| does not seem to be rebuilt consistently after changes. Do
I need to add this new file to build system?
* Bikeshed on what the best PRNG is in 2026
* |make regen| puts the functions prototypes in a weird location "Used
in locale.c and perl.c"
* Add an option to get a random integer? |rand64()|?
Alternate options
* We don't do anything. |rand()| is "good enough"
* Point users at CPAN. |Random::Simple| is a drop-in replacement for
|rand()| and |srand()| already
-- Scottchiefbaker