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
}
TickCount
value undoubtedly increased in the time between assigning the current tick count to$tick1
and resolving + executingGet-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, necessarilyRandom
class in mscorlib andGetRandomCommand
in Microsoft.PowerShell.Commands.Utility, you'll find thatGetRandomCommand
does not wrapRandom
, but initializes it's ownRandomNumberGenerator
, bypassing theRandom(Environment.TickCount)
initialization that you'd expect fromRandom
altogether