horns
horns

Reputation: 1933

What is the difference between Get-Random and random

While experimenting with a small script, it suddenly became much slower. I realized that I had substituted random for get-random, assuming it was an alias.

Compare the following outputs:

measure-command { (0..1000) | % { get-random } }
...
Seconds           : 0
Milliseconds      : 86
...

vs

measure-command { (0..1000) | % { random } }
...
Seconds           : 44
Milliseconds      : 192
...

It appears that random is ~50x slower than get-random. It appears that random is not an alias of get-random, even though it seems to the same parameters. get-alias random and get-command random both return an error that random cannot be not found.

TL;DR

random is not get-random, what is it?

Upvotes: 2

Views: 269

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174525

random is Get-Random

When PowerShell cannot resolve a 1-word command to a function/alias/executable, it acts as if the Get verb is implied.

This works for any other Get-* cmdlet as well. Try some of these in powershell.exe:

item .
childitem $env:USERPROFILE
help random
content $env:SystemRoot\System32\drivers\etc\hosts

I'm not actually sure this is mentioned in the help files, but it has been the case since PowerShell version 1.0, as described in the 2006 book "Monad (AKA. PowerShell): Introducing the MSH Command Shell and Language":

All nouns have a default verb, get, which is assumed if no verb is given. In other words, the command process will behave in exactly the same manner as get-process

Upvotes: 4

Related Questions