dotnet add package FsRandom.Pcg --version 1.4.0
NuGet\Install-Package FsRandom.Pcg -Version 1.4.0
<PackageReference Include="FsRandom.Pcg" Version="1.4.0" />
paket add FsRandom.Pcg --version 1.4.0
#r "nuget: FsRandom.Pcg, 1.4.0"
// Install FsRandom.Pcg as a Cake Addin #addin nuget:?package=FsRandom.Pcg&version=1.4.0 // Install FsRandom.Pcg as a Cake Tool #tool nuget:?package=FsRandom.Pcg&version=1.4.0
The PCG Pseudo-Random Number Generator for F#
PCG is a family of simple fast space-efficient statistically good algorithms for random number generation. Unlike many general-purpose RNGs, they are also hard to predict. You can read more about PCG here.
System.Random is not so good
System.Random has some problems that might be unsuitable for some users.
It uses an obscure algorithm to generate random numbers.
The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.
Try to google it. And then, try to google "Mersenne Twister" or "linear congruential generator". Did you see the diference? The obscurity of this algorithm can be important. While it is designed by a well-respected computer scientist, it's very possible that its output is not random enough, or even worse, it can be easily predicted. Imagine that you are playing a game, and your opponent can predict the roll of a dice or the order of a shuffled deck. Predictability directly contradicts the very definition of randomness. While no software can generate completely random material, it can give a decent try; and it's doing well. Furthermore,
System.Random's implementation has a serious defect.
This is more specific to F# users, but, regardless of the underlying algorithm, the
System.Randomclass has mutable state, an antipattern of functional programming. For example, with immutable values, generating the same random stream again might be a little hard in a stateful, imperative language like C#. Uses of
System.Randomhave an inherent cost of adding side effects, something functional programmers are scared of. But don't worry. F# has a good library for functional random number generation.
PCG is better
Taken from here (modified):
- It's really easy to use, and yet its very flexible and offers powerful features (not all of them are yet available on this library) (including some that allow you to perform silly party tricks).
- It's very fast, and can occupy very little space (only 32 bytes of state, in comparison with
System.Random's 232 bytes of state, and with Mersenne Twister's 2.5 KiB of internal state).
- It has small code size.
- It's performance in statistical tests is excellent (see the PCG paper for full details).
- It's much less predictable and thus more secure than most generators, although I would not advise it for cryptographic usage.
- The reference implementation is open source software, with a permissive license (the Apache license).
The documentation of the library is in the wiki. You can also check the source code comments.
|Product||Versions Compatible and additional computed target framework versions.|
|.NET||net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed.|
|.NET Core||netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed.|
|.NET Standard||netstandard2.0 is compatible. netstandard2.1 was computed.|
|.NET Framework||net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed.|
|MonoAndroid||monoandroid was computed.|
|MonoMac||monomac was computed.|
|MonoTouch||monotouch was computed.|
|Tizen||tizen40 was computed. tizen60 was computed.|
|Xamarin.iOS||xamarinios was computed.|
|Xamarin.Mac||xamarinmac was computed.|
|Xamarin.TVOS||xamarintvos was computed.|
|Xamarin.WatchOS||xamarinwatchos was computed.|
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
Integrated the source code of FsRandom into the project.
FsRandom got some improvements (`ref` became `mutable` and so on).
The library now targets only .NET Standard 2.0.
Today used to be a wonderful day. Now it shouldn't be anything of importance.