{"id":69783,"date":"2005-05-18T11:22:00","date_gmt":"2005-05-18T11:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/18\/how-can-i-generate-random-numbers-using-a-script\/"},"modified":"2005-05-18T11:22:00","modified_gmt":"2005-05-18T11:22:00","slug":"how-can-i-generate-random-numbers-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-generate-random-numbers-using-a-script\/","title":{"rendered":"How Can I Generate Random Numbers Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Our auditors have suggested that we randomly pick projects to examine and ensure that each project was completed and signed off per company policy. Because these projects are numbered sequentially I thought maybe we could write a script that would randomly select the project numbers for us to audit. Can you help us with that?<BR><BR>&#8212; PD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, PD. You know, if you were looking for organized, consistent, and reliable we\u2019d have to say, \u201cNo, sorry, we can\u2019t help you.\u201d But seeing as how you\u2019re looking for random and unpredictable, well, you came to the right place.<\/P>\n<P>As it turns out, VBScript has a built-in function (with the catchy name <B>Rnd<\/B>) designed solely to return random numbers. Let\u2019s take a look at a script that generates 5 random numbers, each one between 1 and 100. We\u2019ll then explain how the script works:<\/P><PRE class=\"codeSample\">intHighNumber = 100\nintLowNumber = 1<\/p>\n<p>For i = 1 to 5\n    Randomize\n    intNumber = Int((intHighNumber &#8211; intLowNumber + 1) * Rnd + intLowNumber)\n    Wscript.Echo intNumber\nNext\n<\/PRE>\n<P>As you can see, we begin by assigning values to two different variables: intHighNumber (the highest number in our range) and intLowNumber (the lowest number in our range). What if we wanted to generate random numbers between 37 and 956? No problem: we\u2019d simply adjust the values of intHighNumber and intLowNumber:<\/P><PRE class=\"codeSample\">intHighNumber = 956\nintLowNumber = 37\n<\/PRE>\n<P>Next we set up a For Next loop that loops around 5 times. Inside the loop we find the following three lines of code:<\/P><PRE class=\"codeSample\">Randomize\nintNumber = Int((intHighNumber &#8211; intLowNumber + 1) * Rnd + intLowNumber)\nWscript.Echo intNumber\n<\/PRE>\n<P>The first line &#8211; consisting solely of the <B>Randomize<\/B> statement &#8211; is very important. This function uses the system time to provide a \u201cseed\u201d value to the Rnd function. Because the system time will always be different this means that the Rnd function will be more likely to generate a truly random number. In fact, try running this script, one that <I>doesn\u2019t<\/I> use the Randomize statement:<\/P><PRE class=\"codeSample\">intHighNumber = 100\nintLowNumber = 1<\/p>\n<p>intNumber = Int((intHighNumber &#8211; intLowNumber + 1) * Rnd + intLowNumber)\nWscript.Echo intNumber\n<\/PRE>\n<P>No matter how many times you run this you\u2019ll get the same \u201crandom\u201d number. (On our test computer, we get <B>71<\/B> each time.)<\/P>\n<P>Our next line of code actually generates the random number:<\/P><PRE class=\"codeSample\">intNumber = Int((intHighNumber &#8211; intLowNumber + 1) * Rnd + intLowNumber)\n<\/PRE>\n<P>The Rnd function has an algorithm it uses to generate random numbers; all we have to do is plug our two variables (intHighNumber and intLowNumber) into the equation. In addition, we use the Int function to ensure that we get back an integer rather than a decimal number. It\u2019s an odd-looking little line of code, but you don\u2019t have to worry about it; just plug the variables into the right spots and have at it.<\/P>\n<P>That\u2019s pretty much it; we echo back the returned number and then loop around and generate the next random number. When we\u2019re all done we should get back a list of numbers similar to this:<\/P><PRE class=\"codeSample\">82\n15\n92\n32\n13\n<\/PRE>\n<P>By the way, could you use this approach to generate numbers for playing the lottery? Yes. Would that make it more likely that you\u2019d pick the winning numbers? Well, let\u2019s put it this way: the Scripting Guys still have to come in to work each and every day. You can decide for yourself whether we\u2019d do that had we written a script that would win us the $100 million PowerBall jackpot.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Our auditors have suggested that we randomly pick projects to examine and ensure that each project was completed and signed off per company policy. Because these projects are numbered sequentially I thought maybe we could write a script that would randomly select the project numbers for us to audit. Can you help [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,5],"class_list":["post-69783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Our auditors have suggested that we randomly pick projects to examine and ensure that each project was completed and signed off per company policy. Because these projects are numbered sequentially I thought maybe we could write a script that would randomly select the project numbers for us to audit. Can you help [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}