dimanche 16 juillet 2017

What PRNG does Get-Random in PowerShell 5.1 Use?

Is there any documentation on what RNG algorithm PowerShell's Get-Random cmdlet officially uses in PowerShell 5.1?

I did some investigating (via decompiling), and it seems Get-Random is just a wrapper for the native .NET Random class. I can confirm this by getting the same values on PowerShell 2.0 (Windows 7) vs C# (targeting .NET 4.5.2). However, Powershell 5.1 (Windows 10) seems to output different numbers.

PowerShell 2.0:

Get-Random -SetSeed 0 -Minimum 0 -Maximum 2147483647
# Produces 1559595546

PowerShell 5.1:

Get-Random -SetSeed 0 -Minimum 0 -Maximum 2147483647
# Produces: 1866861594

C#:

new Random(0).Next(0, 2147483647);
# Produces 1559595546

I did read that after PowerShell 2.0, Get-Random is supposed to support 64-bit numbers, but I set the minimum and maximum above to the 32-bit range for proper testing. Even different seeds, or altering the ranges to something like [0, 100] still yields different results on PowerShell 5.1.

My end goal is basically trying to reproduce random numbers produced in PowerShell 5.1 in either C++ or C# for sake of performance. I already have the C# Random class translated to C++.




Aucun commentaire:

Enregistrer un commentaire