{"id":17824,"date":"2008-12-23T07:40:31","date_gmt":"2008-12-23T15:40:31","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17824"},"modified":"2019-06-07T07:42:18","modified_gmt":"2019-06-07T15:42:18","slug":"dreaming-in-powershell-v2-lottery-numbers-with-get-random","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/dreaming-in-powershell-v2-lottery-numbers-with-get-random\/","title":{"rendered":"Dreaming In PowerShell V2 : Lottery Numbers with Get-Random"},"content":{"rendered":"<p><a href=\"http:\/\/powershell.com\/cs\/blogs\/tobias\/archive\/2008\/12\/22\/unique-lottery-numbers-with-advanced-array-functionality.aspx\">Tobias Weltner writes a blog called Dreaming In PowerShell, and he recently posted a way to get a list of unique lottery numbers with PowerShell.<\/a>&#160; Dreaming In PowerShell is a cool blog, and the post is interesting, but it makes an assertion that&#8217;s no longer true in V2.&#160; He uses System.Random to create the random numbers because he asserts that there isn&#8217;t a cmdlet to Get random numbers.&#160; Get-Random is a V2 cmdlet that can not only give you random numbers, but can give you unique random numbers within a range<\/p>\n<p>My CTP3 version is on the left, Tobias&#8217; V1 version is on the right:<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"928\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">My CTP3 Version<\/td>\n<td valign=\"top\" width=\"404\">Tobias&#8217; V1 version:<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<pre>function Get-RandomNumbers($minimum = 1, $maximum = 49, $number = 20)\r\n{\r\n#.Synopsis\r\n#   Gets a series of unique random numbers\r\n#.Description\r\n#   Gets a series of unique random numbers between a minimum and a maximum\r\n#.Parameter number\r\n#   The number of unique numbers needed\r\n#.Parameter minimum\r\n#   The lowest number the random numbers could contain\r\n#.Parameter maximum\r\n#   The highest number the random numbers could contain\r\n#.Link\r\n#   Get-Random\r\n#.Example\r\n#   Get-RandomNumbers 10 1 100\r\n    $minimum..$maximum | \r\n        Get-Random -Count $number |\r\n        Sort-Object\r\n}<\/pre>\n<\/td>\n<td valign=\"top\" width=\"404\">\n<pre>function Get-RandomNumbers($minimum = 1, $maximum = 49, $number = 20) {\r\n    $random = New-Object System.Random\r\n    $result = @()\r\n    do {\r\n        $randomnumber = $random.Next($minimum,$maximum)\r\n        if ($result -notcontains $randomnumber) {\r\n            $result += $randomnumber\r\n        }\r\n    } while ($result.count -lt $number)\r\n    $result = $result | Sort-Object\r\n    $result\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The core function is a lot shorter (3 lines in CTP3 to 10 lines in V1), but my CTP3 version has inline help.&#160; This means not only can I easily Get unique random numbers, I can also get help about getting unique random numbers.<\/p>\n<p>While I&#8217;m showing Get-Random with a bunch of numbers, you can easily use the same thing to select from a list of words.&#160; Check out this pipeline that gives you 5 random verbs to explore<\/p>\n<pre>Get-Command -type Cmdlet |\r\n        Group-Object Verb | \r\n        Foreach-Object { $_.Name } |\r\n        Get-Random -count 5<\/pre>\n<p>Isn&#8217;t CTP3 Fun?<\/p>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tobias Weltner writes a blog called Dreaming In PowerShell, and he recently posted a way to get a list of unique lottery numbers with PowerShell.&#160; Dreaming In PowerShell is a cool blog, and the post is interesting, but it makes an assertion that&#8217;s no longer true in V2.&#160; He uses System.Random to create the random [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[97,137,180,181,202,330],"class_list":["post-17824","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-advanced-functions","tag-ctp3","tag-get-random","tag-get-randomnumbers","tag-inline-help","tag-tobias-weltner"],"acf":[],"blog_post_summary":"<p>Tobias Weltner writes a blog called Dreaming In PowerShell, and he recently posted a way to get a list of unique lottery numbers with PowerShell.&#160; Dreaming In PowerShell is a cool blog, and the post is interesting, but it makes an assertion that&#8217;s no longer true in V2.&#160; He uses System.Random to create the random [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=17824"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17824\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=17824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}