PcgRandom 1.2.0
dotnet add package PcgRandom --version 1.2.0
NuGet\Install-Package PcgRandom -Version 1.2.0
<PackageReference Include="PcgRandom" Version="1.2.0" />
paket add PcgRandom --version 1.2.0
#r "nuget: PcgRandom, 1.2.0"
// Install PcgRandom as a Cake Addin #addin nuget:?package=PcgRandom&version=1.2.0 // Install PcgRandom as a Cake Tool #tool nuget:?package=PcgRandom&version=1.2.0
About
PcgRandom
provides an implementation of Random
that generates pseudorandom numbers using the PCG family of random number generators. It is a .NET port (written entirely in C#) of the C library by Melissa O'Neill. You can use a PcgRandom
instance anywhere you would use a Random
instance.
Basic Usage
using Pcg;
// create a new random number generator based on the current time
Random random = new PcgRandom();
// or with a seed (to generate the same sequence of numbers)
// random = new PcgRandom(1);
// generate a random non-negative 32-bit integer
Console.WriteLine(random.Next());
// generate a random non-negative 64-bit integer
Console.WriteLine(random.NextInt64());
// generate a random number in a range, e.g., a dice roll; note that
// the upper bound is exclusive, so this generates numbers from 1 to 6
Console.WriteLine(random.Next(1, 7));
// generate a random Boolean
Console.WriteLine(random.Next(2) == 1);
// fill an array with random bytes
var bytes = new byte[16];
random.NextBytes(bytes);
Console.WriteLine(Convert.ToHexString(bytes));
// generate a random floating-point number
Console.WriteLine(random.NextDouble());
// generate a random floating point number from 5 to 15
Console.WriteLine(5.0 + random.NextDouble() * 10);
The above code will produce output similar to the following:
359607667
2470866491422793932
5
True
F0F1E514E052C05868664FA43BA9285D
0.3241723496466875
14.117484646849334
Low-Level API
PcgRandom
also provides a low-level API that is roughly equivalent to the the PCG C High-Level API.
The Pcg32
class is equivalent to the C pcg32_random_t
type:
// create a random Pcg32 instance, specifying a seed and a sequence identifier
var pcg32 = new Pcg32((ulong) random.NextInt64(), (ulong) random.NextInt64());
// generate a random unsigned 32-bit integer
Console.WriteLine(pcg32.GenerateNext());
// skip over 300 numbers in the sequence (without generating them), then generate the next random number
pcg32.Advance(300);
Console.WriteLine(pcg32.GenerateNext());
// advance the state all the way around so that the same number is generated
pcg32.Advance(ulong.MaxValue);
Console.WriteLine(pcg32.GenerateNext());
The the Pcg32Single
class is equivalent to the C pcg32s_random_t
type:
// Pcg32Single takes just a seed; there is only one sequence
var pcg32Single = new Pcg32Single((ulong) random.NextInt64());
// the API is the same as Pcg32
Console.WriteLine(pcg32Single.GenerateNext());
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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-browser 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 | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.0 is compatible. netstandard1.1 was computed. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net45 was computed. net451 was computed. net452 was computed. net46 was computed. 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 | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Windows Phone | wp8 was computed. wp81 was computed. wpa81 was computed. |
Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 1.0
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.