Skip to content

Proposal to replace the algorithm used in Random Class. #18996

Closed
@danmoseley

Description

@danmoseley

@teo-tsirpanis commented on Sat Oct 15 2016

According to MSDN documentation, the Random class is using

a modified version of Donald E. Knuth's subtractive random number generator algorithm.

This algorithm is somewhat obscure. I have found only this paper that can be said that refers to this algorithm.

Therefore, I propose a change of the default RNG in Random class to the Permuted Cogruential Generator(PCG). According to its site, PCG is:

  • More difficult to predict
  • Smaller both in code size (InternalSample will be only five lines of code and Random's constructor will be another five lines)
    and in memory usage (just 16 bytes in comparison with the 232 bytes the existing RNG uses)
  • More statistically random

It can also provide some other useful features.
There is a more detailed comparison of PCG and other RNGs here.

I submitted dotnet/coreclr#7477 to apply these changes, but I was told to temporarily close it and to open an issue instead for further discussion.
The implementation is based on this one.
It has also added some constructors to Random class that allow it to be seeded with the longer ulong seeds PCG allows. It also implements multiple codebooks. The original constructors are retained, so I guess this isn't a breaking API change.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions