Get-Random

Get-Random

  • Comments 7

I saw THIS posting over on Mark Minasi's forums that caught my eye.  It gives you a random help file under the motto of: "A powershell help file a day, keeps Don Jones away".  :-)

Don responded pointing out one of our new V2 cmdlets:  Get-Random.  Since that cat is out of the bag and Get-Random is so cool, let's explore it:

PS>Get-Help Get-Random

NAME
    Get-Random

SYNOPSIS
    Gets a random number or selects objects randomly from a collection.


SYNTAX
    Get-Random [-Count [<Uint32>]] [[-InputObject] [<psobject>]] [-SetSeed [<in
    t>]] [<CommonParameters>]

    Get-Random [-Minimum [<Object>]] [[-Maximum] [<Object>]] [-SetSeed [<int>]]
     [<CommonParameters>]


DETAILED DESCRIPTION
    The Get-Random cmdlet gets a randomly selected number. If you submit a coll
    ection of objects to Get-Random, it gets one or more randomly selected obje
    cts from the collection.

    Without parameters or input, "Get-Random" returns a randomly selected 32-bi
    t unsigned integer between 0 and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647)
    .

    You can use the parameters of Get-Random to specify a seed number, minimum
    and maximum values, and the number of objects returned from a submitted col
    lection.


RELATED LINKS


REMARKS
    For more information, type: "get-help Get-Random -detailed".
    For technical information, type: "get-help Get-Random -full".

 

PS> # Get A random number
PS> Get-Random
1163375074
PS>
PS> # Get a random number within a range
PS> Get-Random -min 10 -max 15
13
PS>
PS> # Get a random element
PS> "Tom","Dick","Harry" |Get-Random
Dick
PS> # Get a set of random elements
PS> "Tom","Dick","Harry" |Get-Random -count 3
Tom
Harry
Dick
PS> # Is it really random?  yup
PS> "Tom","Dick","Harry" |Get-Random -count 3
Dick
Harry
Tom
PS> # It works with any set of objects
PS> gps |get-Random -Count 2

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    361      26    51316      55864   151   225.73   1700 svchost
   1570      16    68756      30300   276   457.43   2424 SearchIndexer


PS> gps |get-Random -Count 2

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    316       8     3004       7368    40    36.61    604 services
    209       9    75740     137232   197 1,067.36   2760 dwm

 

At the end of the day, the cmdlets that manage the OS features need to come from those feature teams so you should not expect PowerShell to deliver a ton of those.  For instance, the AD team needs to deliver the AD cmdlets, the network team needs to deliver networking Cmdlets.  If you are not getting the cmdlets you need, you should knock on the doors of those feature teams.  Don't suffer in silence, complain.

PowerShell will deliver some management cmdlets but by and large, we are going to focus our efforts on utility cmdlets that work against anyones objects and make it a good deal for those feature teams to develop cmdlets.  Get-Random is just such a cmdlet.  I'll bet you a dollar to a doughnut that no feature team would ever prioritize Get-Random but it turns out that all of their customers will use it at some point.  PowerShell delivers it and thus the feature team's customers benefit.  That's the deal - you invest in writting cmdlets/providers and we invest in ensuring that that was a great investement by delivering a ton of functions to your customers.  How can you go wrong with that?

Enjoy! 

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
  • Hi Jeffrey,

    here you got one bit - what about cmdlet for handling reparse points (junction, mounting points, probably symlinks\hardlinks in Vista)??? If you will implement it, please please dont do it only for Vista, but also for XP...

    Thanks,

    Martin

  • Any info on which RNG is behind that cmdlet?

  • In the detailed description you need to change "unsigned integer" to "signed integer".

  • @Johannes,

    I'd simply look at the details of System.Random from the .NET 2.0 Framework.

  • Does V2 also include Set-Random so I'll finally be able to win the lottery? :)

    --

    greetings

    dreeschkind

  • I assume the no seed version keeps one System.Random object around instead of recreating each invoke? If I remember correctly, Random is seeded with GetTickCount(), meaning you would get the same values back until the next msec.

  • One of the new cmdlets in PowerShell CTP2 is Get-Random . Get-Random gets a random number or selects

Page 1 of 1 (7 items)