4

The documentation for the Powershell Get-Random cmdlet suggests that the seed is the system tick count but, if the following test is performed, the value of $rand1 is not replicated in the output of the for() loop and so appears to be independent of TickCount. Why is this so?

# generate a random int32 - should automatically use the tickcount as seed?
$tick1 = ([Environment]::TickCount)
$rand1 = Get-Random
Write-Host $rand1
$tick2 = ([Environment]::TickCount)

# generate seeded randoms with all possible values used to generate $rand1
for ($i = $tick1; $i -le $tick2; $i++) {
    $rand2 = Get-Random -SetSeed $i
    Write-Host $rand2
}
6
  • 1
    2 things: The TickCount value undoubtedly increased in the time between assigning the current tick count to $tick1 and resolving + executing Get-Random. Additionally, the help file says that it uses the system clock to calculate the seed, not that the seed is the value of the system clock, necessarily Dec 17, 2015 at 10:02
  • Regarding the first point: that's why I collected a prior and post tick count ($tick1 and $tick2) and then looped over all possible values which were available to Get-Random at the time that $rand1 is generated, so I think that's accounted for? And oddly enough Get-Random is simply a wrapper for the .net Random function. I have tested that function and it does work exactly as expected - it genuinely does use the tick count. Dec 17, 2015 at 10:07
  • You're right - there must be something extra going on in the PowerShell cmdlet, but when examining it using powershell reflector I can't see that it's doing anything other than calling and passing on the .net generator. It's a bit obscure. Dec 17, 2015 at 10:11
  • If you open ILSpy and compare the Random class in mscorlib and GetRandomCommand in Microsoft.PowerShell.Commands.Utility, you'll find that GetRandomCommand does not wrap Random, but initializes it's own RandomNumberGenerator, bypassing the Random(Environment.TickCount) initialization that you'd expect from Random altogether Dec 17, 2015 at 10:26
  • That's really useful Mathias - thanks, I've been looking at the cmdlet using Reflector but have just had a look using ILSpy too - I can't see a definition for RandomNumberGenereator though - it just appears that GetRandomCommand is calling Random in the BeginProcessing() function? Dec 17, 2015 at 13:33

2 Answers 2

3

Get-Random isn't necessarily seeded directly when you use it (certainly not every time you use it), it could be either at process startup, or when you first use it in a session. PRNGs shouldn't be seeded more than once, usually.

Also, as Mathias notes in a comment, it doesn't necessarily use the value of Environment.TickCount directly. You could use ILSpy to look at the source of the relevant assembly to find out how exactly it's done. I won't do that for you since I'm a contributor to an open-source PowerShell implementation.

2

So, I was curious about this today and now that PowerShell is open source I thought I'd just go look at the source code...

Get-Random does not use [Environment]::TickCount as the seed. In fact the only time it doesn't use [System.Security.Cryptography.RandomNumberGenerator]::Create() is when you manually specify a seed.

It has 2 constructors...

internal class PolymorphicRandomNumberGenerator
{
    public PolymorphicRandomNumberGenerator()
    {
        _cryptographicGenerator = RandomNumberGenerator.Create();
        _pseudoGenerator = null;
    }

    internal PolymorphicRandomNumberGenerator(int seed)
    {
        _cryptographicGenerator = null;
        _pseudoGenerator = new Random(seed);
    }
    ...

It only calls the constructor with a seed if SetSeed has a value in the Begin process:

/// <summary>
/// This method implements the BeginProcessing method for get-random command.
/// </summary>
protected override void BeginProcessing()
{
    if (SetSeed.HasValue)
    {
        Generator = new PolymorphicRandomNumberGenerator(SetSeed.Value);
    }
    ... 

That would set _pseudoGenerator using System.Random

Otherwise it only ever calls the public constructor without a seed...which sets _cryptographicGenerator using System.Security.Cryptography.RandomNumberGenerator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.