<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'Array', 'Random', and '-contains'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=Array,Random,-contains&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'Array', 'Random', and '-contains'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Unique Lottery Numbers With Advanced Array Functionality</title><link>http://powershell.com/cs/blogs/tobias/archive/2008/12/22/unique-lottery-numbers-with-advanced-array-functionality.aspx</link><pubDate>Mon, 22 Dec 2008 06:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:622</guid><dc:creator>Tobias Weltner</dc:creator><description>&lt;p&gt;Recently in a workshop, we wanted to create a little PowerShell script&amp;nbsp;to return&amp;nbsp;nine unique random numbers. Since all participants were experienced VBScript scripters, they quickly came up with a solution. This solution grew bigger and bigger and more complicated with every new detail (&amp;quot;the random numbers need to be unique&amp;quot;).&lt;/p&gt;
&lt;p&gt;Finally, we created a little PowerShell script which I want to share with you. It touches a number of efficient PowerShell strategies and demonstrates why PowerShell code can be very short and efficient and why it does not always pay off to translate older scripts 1:1 to PowerShell.&lt;/p&gt;
&lt;h2&gt;Getting Random Numbers&lt;/h2&gt;
&lt;p&gt;The first thing we need are random numbers. Since there is no built-in cmdlet that provides random numbers, PowerShell allows you to &amp;quot;cross the red line of managed commands&amp;quot; and enter the wild and wonderful world of the developers by using .NET code directly. &lt;/p&gt;
&lt;p&gt;The hardest thing here is to know the name of the .NET type providing the kind of service you are looking for. The .NET type &lt;strong&gt;System.Random&lt;/strong&gt; is the random number generator you need, and once you derive an instance (a real object) from that type, you can use the &lt;strong&gt;Next()&lt;/strong&gt; method to create random numbers:&lt;/p&gt;
&lt;div style="font-family:&amp;#39;Consolas&amp;#39;;font-size:12;background:#012456;"&gt;&lt;span style="color:#eeedf0;background-color:#012456;"&gt;PS&amp;gt;&amp;nbsp;$random&amp;nbsp;=&amp;nbsp;New-Object&amp;nbsp;System.Random&lt;br /&gt;PS&amp;gt;&amp;nbsp;$random.Next()&lt;br /&gt;279622301&lt;br /&gt;PS&amp;gt;&amp;nbsp;$random.Next(1,6)&lt;br /&gt;2&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Next()&lt;/strong&gt; actually gives you a true random number. To scale this random number into a specific value range, add the minimum and maximum value you want to use. &lt;strong&gt;Next(1,6)&lt;/strong&gt; is your electronic dice and returns a random number between 1 and 6.&lt;/p&gt;
&lt;h2&gt;Getting 9 Random Numbers&lt;/h2&gt;
&lt;p&gt;Since we need more than one random number, what we need next is a &lt;strong&gt;loop&lt;/strong&gt;. To return nine random numbers out of 49, you could use a for loop like this:&lt;/p&gt;
&lt;div style="font-family:&amp;#39;Consolas&amp;#39;;font-size:12;background:#012456;"&gt;&lt;span style="color:#eeedf0;background-color:#012456;"&gt;PS&amp;gt;&amp;nbsp;for&amp;nbsp;($x=1;&amp;nbsp;$x&amp;nbsp;-le&amp;nbsp;9;&amp;nbsp;$x++)&amp;nbsp;{&amp;nbsp;$random.Next(1,49)&amp;nbsp;}&lt;br /&gt;31&lt;br /&gt;17&lt;br /&gt;40&lt;br /&gt;48&lt;br /&gt;11&lt;br /&gt;39&lt;br /&gt;19&lt;br /&gt;23&lt;br /&gt;25&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;This kind of loop uses a counter variable &lt;strong&gt;$x&lt;/strong&gt; which is initialized to 1 and then incremented by 1 (&lt;strong&gt;$x++&lt;/strong&gt;). The loop continues to execute the script block while the condition is met (&lt;strong&gt;$x -le 9&lt;/strong&gt;). A more readable form of this loop could look like this:&lt;/p&gt;
&lt;div style="font-family:&amp;#39;Consolas&amp;#39;;font-size:12;background:#012456;"&gt;&lt;span style="color:#eeedf0;background-color:#012456;"&gt;PS&amp;gt;&amp;nbsp;1..9&amp;nbsp;|&amp;nbsp;ForEach-Object&amp;nbsp;{&amp;nbsp;$random.Next(1,49)&amp;nbsp;}&amp;nbsp;|&amp;nbsp;Sort-Object&lt;br /&gt;12&lt;br /&gt;20&lt;br /&gt;21&lt;br /&gt;22&lt;br /&gt;22&lt;br /&gt;28&lt;br /&gt;28&lt;br /&gt;39&lt;br /&gt;42&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Note how &lt;strong&gt;Sort-Object&lt;/strong&gt; sorts the result as well. This reveals a flaw in this approach: there can be duplicate random numbers in the result since each drawing is independent of the previous drawings.&lt;/p&gt;
&lt;h2&gt;Getting Unique Random Numbers&lt;/h2&gt;
&lt;p&gt;To get a list of truly unique random numbers, we need to keep track of the numbers we already drew to make sure each new random number is new. &lt;/p&gt;
&lt;p&gt;There are plenty of ways to do this but a very easy way is to use a PowerShell &lt;strong&gt;array&lt;/strong&gt;. We need to change the loop construct as well since this time, we do not know beforehand how many random numbers we need to draw (depending on how many duplicate entries we draw). &lt;/p&gt;
&lt;p&gt;Whenever you need to take care of runtime environments you should choose the &lt;strong&gt;do...while&lt;/strong&gt; loop. It loops the script block until some exit condition is met. Let&amp;#39;s first draw nine random numbers and place them in a result array:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="var"&gt;$random&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="verbnoun"&gt;New-Object&lt;/span&gt; &lt;span class="namespace"&gt;System.Random&lt;/span&gt;&lt;br /&gt;&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; @()&lt;br /&gt;&lt;br /&gt;&lt;span class="keyword"&gt;do&lt;/span&gt; {&lt;br /&gt;&lt;br /&gt;	&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;+=&lt;/span&gt; &lt;span class="var"&gt;$random&lt;/span&gt;.&lt;span class="method"&gt;Next&lt;/span&gt;(1,49)&lt;br /&gt;&lt;br /&gt;} &lt;span class="keyword"&gt;while&lt;/span&gt; (&lt;span class="var"&gt;$result&lt;/span&gt;.&lt;span class="method"&gt;count&lt;/span&gt; &lt;span class="op"&gt;-le&lt;/span&gt; 8)&lt;br /&gt;&lt;br /&gt;&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="var"&gt;$result&lt;/span&gt; | &lt;span class="verbnoun"&gt;Sort-Object&lt;/span&gt;&lt;br /&gt;&lt;span class="var"&gt;$result&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;@() &lt;/strong&gt;creates a new empty array. Unlike other script languages, PowerShell makes it very easy to add additional elements to that array. Simply assign a new value to the array, and it grows by that element.&lt;/p&gt;
&lt;p&gt;The loop runs until the &lt;strong&gt;$result&lt;/strong&gt; array contains more than&amp;nbsp;8 elements. Note that this is a footer-based loop. In case you need a header-based loop where the loop condition is checked before the loop enters, replace the &lt;strong&gt;do&lt;/strong&gt; statement with your &lt;strong&gt;while&lt;/strong&gt; statement.&lt;/p&gt;
&lt;p&gt;At the end of this little script, the array is sorted using &lt;strong&gt;Sort-Object&lt;/strong&gt; and then outputted.&lt;/p&gt;
&lt;p&gt;However, this script still can deliver duplicate random numbers. So we still need a way of finding out whether a random number was already drawn. This job is done by the &lt;strong&gt;-contains&lt;/strong&gt; operator. It checks whether an array already contains an entry:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="var"&gt;$random&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="verbnoun"&gt;New-Object&lt;/span&gt; &lt;span class="namespace"&gt;System.Random&lt;/span&gt;&lt;br /&gt;&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; @()&lt;br /&gt;&lt;br /&gt;&lt;span class="keyword"&gt;do&lt;/span&gt; {&lt;br /&gt;	&lt;br /&gt;	&lt;span class="var"&gt;$randomnumber&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="var"&gt;$random&lt;/span&gt;.&lt;span class="method"&gt;Next&lt;/span&gt;(1,49)&lt;br /&gt;	&lt;span class="keyword"&gt;if&lt;/span&gt; (&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;-notcontains&lt;/span&gt; &lt;span class="var"&gt;$randomnumber&lt;/span&gt;) {&lt;br /&gt;		&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;+=&lt;/span&gt; &lt;span class="var"&gt;$randomnumber&lt;/span&gt;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;&lt;br /&gt;} &lt;span class="keyword"&gt;while&lt;/span&gt; (&lt;span class="var"&gt;$result&lt;/span&gt;.&lt;span class="method"&gt;count&lt;/span&gt; &lt;span class="op"&gt;-le&lt;/span&gt; 8)&lt;br /&gt;&lt;br /&gt;&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="var"&gt;$result&lt;/span&gt; | &lt;span class="verbnoun"&gt;Sort-Object&lt;/span&gt;&lt;br /&gt;&lt;span class="var"&gt;$result&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To find out whether the &lt;strong&gt;$result&lt;/strong&gt; array already contains your newly drawn random number, the new random number is first stored in a separate variable (&lt;strong&gt;$randomnumber&lt;/strong&gt;). Next, a condition is used (&lt;strong&gt;If&lt;/strong&gt;). It checks whether or not the array contains the new random number. &lt;/p&gt;
&lt;p&gt;If it does not contain the number (&lt;strong&gt;-notcontains&lt;/strong&gt;), the script block after the condition is executed and adds the new number to your array. Since the loop continues until the array contains nine elements, you do not need to change anything here. The loop will simply collect new random numbers until you have successfully drawn nine unique numbers.&lt;/p&gt;
&lt;h2&gt;Creating A Function&lt;/h2&gt;
&lt;p&gt;Finally, we can wrap up the code as a new function like &lt;strong&gt;Get-RandomNumbers&lt;/strong&gt;. The following function returns any number of (unique) random numbers in a variable range:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="keyword"&gt;function&lt;/span&gt; &lt;span class="verbnoun"&gt;Get-RandomNumbers&lt;/span&gt;(&lt;span class="var"&gt;$minimum&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; 1, &lt;span class="var"&gt;$maximum&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; 49, &lt;span class="var"&gt;$number&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; 9) {&lt;br /&gt;	&lt;span class="var"&gt;$random&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="verbnoun"&gt;New-Object&lt;/span&gt; &lt;span class="namespace"&gt;System.Random&lt;/span&gt;&lt;br /&gt;	&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; @()&lt;br /&gt;&lt;br /&gt;	&lt;span class="keyword"&gt;do&lt;/span&gt; {&lt;br /&gt;&lt;br /&gt;		&lt;span class="var"&gt;$randomnumber&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="var"&gt;$random&lt;/span&gt;.&lt;span class="method"&gt;Next&lt;/span&gt;(1,49)&lt;br /&gt;		&lt;span class="keyword"&gt;if&lt;/span&gt; (&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;-notcontains&lt;/span&gt; &lt;span class="var"&gt;$randomnumber&lt;/span&gt;) {&lt;br /&gt;			&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;+=&lt;/span&gt; &lt;span class="var"&gt;$randomnumber&lt;/span&gt;&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	} &lt;span class="keyword"&gt;while&lt;/span&gt; (&lt;span class="var"&gt;$result&lt;/span&gt;.&lt;span class="method"&gt;count&lt;/span&gt; &lt;span class="op"&gt;-lt&lt;/span&gt; &lt;span class="var"&gt;$number&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;	&lt;span class="var"&gt;$result&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="var"&gt;$result&lt;/span&gt; | &lt;span class="verbnoun"&gt;Sort-Object&lt;/span&gt;&lt;br /&gt;	&lt;span class="var"&gt;$result&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;So to get five unique random numbers between 10 and 100, call it like this:&lt;/p&gt;
&lt;div style="font-family:&amp;#39;Consolas&amp;#39;;font-size:12;background:#012456;"&gt;&lt;span style="color:#eeedf0;background-color:#012456;"&gt;PS&amp;gt;&amp;nbsp;Get-RandomNumbers&amp;nbsp;-min&amp;nbsp;10&amp;nbsp;-max&amp;nbsp;100&amp;nbsp;5&lt;br /&gt;21&lt;br /&gt;24&lt;br /&gt;31&lt;br /&gt;46&lt;br /&gt;47&lt;br /&gt;PS&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;Learning Points&lt;/h2&gt;
&lt;p&gt;Whenever you need a functionality not provided by &amp;quot;managed&amp;quot; Cmdlets, you can resort to the .NET framework. Simply go to google and search for whatever you need. Chances are you will find the name of the .NET type you need to do the job. Random numbers can be generated with the help of the .NET type &lt;strong&gt;System.Random&lt;/strong&gt;. &lt;a target="_blank" href="http://www.powershellplus.com"&gt;PowerShell Plus&lt;/a&gt; will provide you with a full list of all available .NET types and also provides code completion for the methods like &lt;strong&gt;Next()&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;There are two basic &lt;strong&gt;loop constructs&lt;/strong&gt;. For loops are used when you know at design time how often the loop needs to execute. You can either use the &lt;strong&gt;For()&lt;/strong&gt; statement and use a counter variable, or you can create loops using the pipeline and &lt;strong&gt;Foreach-Object&lt;/strong&gt;. Whenever you do not know at design time how often the loop needs to run, use the &lt;strong&gt;Do...While&lt;/strong&gt; or &lt;strong&gt;Do...Until&lt;/strong&gt; loop. Any loop can be dangerous when you provide a wrong exit condition that never exits the loop.&lt;/p&gt;
&lt;p&gt;If you need to execute code based on a certain condition, use &lt;strong&gt;If&lt;/strong&gt;. Only when the condition you specified evaluates to &lt;strong&gt;$true&lt;/strong&gt; will the script code be executed. This is how the example adds new random numbers to the array.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt; are a great way of storing and examining results. To create an empty array, use the&lt;strong&gt; @()&lt;/strong&gt; construct. There are a number of powerful enhancements to arrays in PowerShell. For example, you can simply add new elements to let the array grow. Each array has a &lt;strong&gt;Count&lt;/strong&gt; property which tells you how many elements are stored inside the array, and the &lt;strong&gt;-contains&lt;/strong&gt; and&lt;strong&gt; -notcontains&lt;/strong&gt; comparison operators check whether the array already contains some given value.&lt;/p&gt;
&lt;p&gt;Merry Xmas to all&lt;/p&gt;
&lt;p&gt;-Tobias&lt;/p&gt;</description></item></channel></rss>