Randomコマンドレット Get-Random 1/236

こんにちは、「Randomコマンドレット」を始めます。

PS (1) > gcm -co cmdlet | random

CommandType     Name         Definition                                         
-----------     ----         ----------                                         
Cmdlet          Out-String   Out-String [-Stream] [-Width <Int32>] [-InputObj...

PowerShell には、236のコマンドレットと呼ばれるコマンドがあるので、
これをランダムに見ていこうと思います。

(1)のコマンドを入力すると、236のうち1つ選ばれて出力されます。

PS (2) > out-string

ランダムの結果、Out-String が選ばれました。
これを実行してみると、何も出力されませんでした。なんでしょう。

PS (3) > Get-Command -CommandType cmdlet | Get-Random

CommandType     Name                    Definition                                         
-----------     ----                    ----------                                         
Cmdlet          Disable-ComputerRestore Disable-ComputerRestore [-Drive] <String[]> [-Ve...

(1)は、(3)の省略した記述になります。

省略 そのまま 説明
gcm Get-Command これは、エイリアス(alias)です。
-co -CommandType パラメータは、ある程度、省略できます。
random Get-Random コマンドは、"Get-"を省略できます。

今度は、ランダムの結果、Disable-ComputerRestore が選ばれました。
これは、なんでしょう?使ったこと無いですね。

PS (4) > man Disable-ComputerRestore

名前
    Disable-ComputerRestore

概要
    指定したファイル システム ドライブのシステム復元機能を無効にします。

構文
    Disable-ComputerRestore [-Drive] <string[]> [-Confirm] [-WhatIf] [<CommonParameters>]

・・・

PowerShellの中で、ヘルプが読めます。
Webで検索すると、MSのTechNetのオンラインヘルプがすぐに出てきます。
(検索結果)http://technet.microsoft.com/ja-jp/library/dd347710.aspx

さて、Randomコマンドレットなので、1回目は、Get-Randomを調べます。

PS (5) > help Get-Random

名前
    Get-Random

概要
    ランダムな数値を取得するか、オブジェクトをコレクションからランダムに選択します。

構文
    Get-Random [-InputObject] <Object[]> [-Count <int>] [-SetSeed <int>] [<CommonParameters>]

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

説明
    Get-Random コマンドレットは、ランダムに選択された数値を取得します。オブジェクトのコレクションを
 Get-Random に送ると、ランダムに選択された 1 つまたは複数のオブジェクトをコレクションから取得します。

    パラメーターまたは入力のない Get-Random コマンドは、0 (ゼロ) 〜 Int32.MaxValue (0x7FFFFFFF、
2,147,483,647) のランダムに選択された 32 ビット符号なし整数を返します。

    Get-Random のパラメーターを使用すると、シード数、最小値と最大値、および送られたコレクションから
返されるオブジェクトの数を指定できます。
・・・

色々書いてます。

早速やってみます。
まずは、そのまま。0〜2,147,483,647までの間でランダムな値が返ってきます。
PS (6) > Get-Random
904529694
PS (7) > Get-Random
1568636065
PS (8) > Get-Random
1361652924

次は、最少と、最大を決めて。
PS (9) > Get-Random -Minimum 1 -Maximum 100
85
PS (10) > Get-Random -Minimum 1 -Maximum 100
86
PS (11) > Get-Random -Minimum 1 -Maximum 100
80
PS (12) > Get-Random -Minimum 1 -Maximum 100
63
PS (13) > Get-Random -Minimum 1 -Maximum 100
26

次は、「オブジェクトをコレクションからランダム」ってことで
配列から一つ選びます。
1..100 は、1から100までの配列を作成します。
PS (14) > 1..100 | Get-Random
92
PS (15) > 1..100 | Get-Random
48
PS (16) > 1..100 | Get-Random
9

SetSeedパラメーターも試してみます。
同じ値が返ってきます。(使わなさそう・・・)
PS (17) > Get-Random -Minimum 1 -Maximum 101 -SetSeed 1
25
PS (18) > 1..100 | Get-Random -SetSeed 1
85
PS (19) > 1..100 | Get-Random -SetSeed 1
85
PS (20) > 1..100 | Get-Random -SetSeed 1
85

Get-Randomを調べるには、先ほどのhelp もしくは man もしくは Get-Helpを呼び出すと、
PowerShell内のヘルプが読めます。
もう一つは、Get-Commandで、詳しい内容を取得できます。

PS (21) > Get-Command -Name Get-Random

CommandType     Name        Definition                                         
-----------     ----        ----------                                         
Cmdlet          Get-Random  Get-Random [[-Maximum] <Object>] [-SetSeed <Null...

ほとんど途切れてしまうので、後ろにパイプラインで fl をつけると全部みれます。

PS (22) > Get-Command -Name Get-Random | fl

Name             : Get-Random
CommandType      : Cmdlet
Definition       : Get-Random [[-Maximum] <Object>] [-SetSeed <Nullable`1>] [-Minimum <Object>] [-Verbose] [-Debug] [-E
                   rrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-Warni
                   ngVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>]
                   Get-Random [-InputObject] <Object[]> [-SetSeed <Nullable`1>] [-Count <Int32>] [-Verbose] [-Debug] [-
                   ErrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-Warn
                   ingVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>]

Path             :
AssemblyInfo     :
DLL              : C:\Windows\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\1.0.0.0__31bf3856ad364e35\Microso
                   ft.PowerShell.Commands.Utility.dll
HelpFile         : Microsoft.PowerShell.Commands.Utility.dll-Help.xml
ParameterSets    : {[[-Maximum] <Object>] [-SetSeed <Nullable`1>] [-Minimum <Object>] [-Verbose] [-Debug] [-ErrorAction
                    <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable
                    <String>] [-OutVariable <String>] [-OutBuffer <Int32>], [-InputObject] <Object[]> [-SetSeed <Nullab
                   le`1>] [-Count <Int32>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <Actio
                   nPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuf
                   fer <Int32>]}
ImplementingType : Microsoft.PowerShell.Commands.GetRandomCommand
Verb             : Get
Noun             : Random

長いですね。

Get-Randomには、2種類の使い方がありました。
  1. ランダムな数値を返す
    • 指定がなければ、0〜2,147,483,647
    • 指定があれば、その範囲で返す。例えば1〜100 (-min 1 -max 100)
  1. オブジェクトをコレクションからランダムに返す
    • 1から100の配列から1つ返す
    • Get-Commamnd -CommandType cmdlet の結果から1つ返す

これは、パラメータセットと言って、1つのコマンドレットに2種類の使い方が
定義されています。
それぞれ名前が付いていて、以下で調べることができます。

PS (23) > Get-Command -Name Get-Random | % {$_.parametersets}

・・・長い

PS (24) > Get-Command -Name Get-Random | % {$_.parametersets}| % {$_.tostring()}

[[-Maximum] <Object>] [-SetSeed <Nullable`1>] [-Minimum <Object>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>]
 [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-Ou
tBuffer <Int32>]
[-InputObject] <Object[]> [-SetSeed <Nullable`1>] [-Count <Int32>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>
] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-O
utBuffer <Int32>]

PS (25) > Get-Command -Name Get-Random | % {$_.parametersets}| % {$_.name}

RandomNumberParameterSet
RandomListItemParameterSet

Get-Random

  1. ランダムな数値を返す (ランダムナンバーパラメータセット)