{"id":577,"date":"2011-10-11T01:42:39","date_gmt":"2011-10-10T20:12:39","guid":{"rendered":"http:\/\/www.automationrepository.com\/?p=577"},"modified":"2011-10-11T01:45:47","modified_gmt":"2011-10-10T20:15:47","slug":"generate-random-numbers-in-qtp","status":"publish","type":"post","link":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/","title":{"rendered":"3 Different Ways to Create Random Numbers in QTP"},"content":{"rendered":"<div style=\"font-family: Verdana;\">\n<p style=\"text-align: justify;\">Many a times you would come across the need to create Random Numbers in your script. And this random number requirement would come in different flavors &#8211; you might need to create a random number of a certain length, or you might want a random number within a specific range, or you might just need<!--more--> any random number without worrying about the range or number length. Whatever be your need, the below mentioned 3 methods should help you find a solution. :&#8211;)<\/p>\n<h3>1. Create a Random Number having a Fixed Length<\/h3>\n<p style=\"text-align: justify;\">I have created a VBScript function that should help you find a random number of any length you want.<br \/>\n<code><br \/>\n<span style=\"color: #008000;\">'===============================================<\/span><br \/>\n<span style=\"color: #008000;\"> 'Function to Create a Random Number of Any Length<\/span><br \/>\n<span style=\"color: #008000;\"> '===============================================<\/span><br \/>\n<span style=\"color: #0000ff;\"> Function<\/span> fnRandomNumber(LengthOfRandomNumber)<\/code><br \/>\n<code><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sMaxVal : sMaxVal = <span style=\"color: #993300;\">\"\"<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> iLength : iLength = LengthOfRandomNumber<\/code><br \/>\n<code><br \/>\n<span style=\"color: #008000;\">'Find the maximum value for the given number of digits<\/span><br \/>\n<span style=\"color: #0000ff;\">For<\/span> iL = 1 to iLength<br \/>\nsMaxVal = sMaxVal &amp; <span style=\"color: #993300;\">\"9\"<\/span><br \/>\n<span style=\"color: #0000ff;\">Next<\/span><br \/>\nsMaxVal = <span style=\"color: #0000ff;\">Int<\/span>(sMaxVal)<\/code><br \/>\n<code><br \/>\n<span style=\"color: #008000;\">'Find Random Value<\/span><br \/>\n<span style=\"color: #0000ff;\">Randomize<\/span><br \/>\niTmp = <span style=\"color: #0000ff;\">Int<\/span>((sMaxVal * <span style=\"color: #0000ff;\">Rnd<\/span>) + 1)<br \/>\n<span style=\"color: #008000;\">'Add Trailing Zeros if required<\/span><br \/>\niLen = <span style=\"color: #0000ff;\">Len<\/span>(iTmp)<br \/>\nfnRandomNumber = iTmp * (10 ^(iLength - iLen))<\/code><br \/>\n<code><br \/>\n<span style=\"color: #0000ff;\"> End Function<\/span><br \/>\n<span style=\"color: #008000;\">'================== End Function =================<\/span><br \/>\n<\/code><br \/>\nTo use this function in your script, all you have to do this call this function with the lenght of the random number you want. Example to get a random number of 6 digits, you can write <strong>myRandomNum = fnRandomNumber(6)<\/strong><\/p>\n<h3>2. Create a Random Number Within a Specific Range<\/h3>\n<p style=\"text-align: justify;\">The first example showed how to create a random number of a fixed size. But what if you need a random number within a specific range? In this second function, all you have to do is to specify the maximum and minimum value. The function would return a value that lies in between the range you specified.<\/p>\n<p style=\"text-align: justify;\"><code><br \/>\n<span style=\"color: #008000;\"> '===========================================================<\/span><br \/>\n<span style=\"color: #008000;\"> 'Function to Create a Random Number within a specified range<\/span><br \/>\n<span style=\"color: #008000;\"> '===========================================================<\/span><br \/>\n<span style=\"color: #0000ff;\">Function<\/span> fnRandomNumberWithinRange(MinimumRange, <code>MaximumRange<\/code>)<\/code><br \/>\n<code><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> iMax : iMax = MaximumRange<br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> iMin : iMin = MinimumRange<\/code><br \/>\n<code><br \/>\n<span style=\"color: #008000;\">'Create Random Number within the Range<\/span><br \/>\n<span style=\"color: #0000ff;\">Randomize<\/span><br \/>\nfnRandomNumberWithinRange = Int(((iMax - iMin) * <span style=\"color: #0000ff;\">Rnd<\/span>) + iMin)<\/code><br \/>\n<code><br \/>\n<span style=\"color: #0000ff;\">End Function<\/span><br \/>\n<span style=\"color: #008000;\">'======================== End Function =====================<\/span><\/code><br \/>\nCalling the above function as <strong>iRandomNum =\u00a0fnRandomNumberWithinRange(1200, 1500)<\/strong> would return a random number between 1200 &amp; 1500.<\/p>\n<h3>3. Create a Random Number with DateTime Stamp<\/h3>\n<p style=\"text-align: justify;\">This method doesn&#8217;t use the Randomize &amp; Rnd functions to create a random number. Instead it uses the Date &amp; Time combination to come up with a unique number. <span style=\"text-decoration: underline;\">One advantage of this method is that here you can be rest assured that whatever number you create using this method, it would never be repeated because the combination of date &amp; time is always unique.<\/span><\/p>\n<p><code><br \/>\n<span style=\"color: #008000;\">'===========================================================<\/span><br \/>\n<span style=\"color: #008000;\"> 'Function to Create a Random Number with DateTime Stamp<\/span><br \/>\n<span style=\"color: #008000;\"> '===========================================================<\/span><br \/>\n<span style=\"color: #0000ff;\">Function<\/span> fnRandomNumberWithDateTimeStamp()<\/code><br \/>\n<code><br \/>\n<span style=\"color: #008000;\">'Find out the current date and time<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sDate : sDate = <span style=\"color: #0000ff;\">Day(Now)<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sMonth : sMonth = <span style=\"color: #0000ff;\">Month(Now)<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sYear : sYear = <span style=\"color: #0000ff;\">Year(Now)<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sHour : sHour = <span style=\"color: #0000ff;\">Hour(Now)<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sMinute : sMinute = <span style=\"color: #0000ff;\">Minute(Now)<\/span><br \/>\n<span style=\"color: #0000ff;\">Dim<\/span> sSecond : sSecond = <span style=\"color: #0000ff;\">Second(Now)<\/span><\/code><br \/>\n<code><br \/>\n<span style=\"color: #008000;\"> 'Create Random Number<\/span><br \/>\nfnRandomNumberWithDateTimeStamp = <span style=\"color: #0000ff;\">Int<\/span>(sDate &amp; sMonth &amp; sYear &amp; sHour &amp; sMinute &amp; sSecond)<\/code><br \/>\n<code><br \/>\n<span style=\"color: #0000ff;\"> End Function<\/span><br \/>\n<span style=\"color: #008000;\">'======================== End Function =====================<\/span><\/code><\/p>\n<p style=\"text-align: justify;\"><em>I hope that the above 3 methods would have helped you find the solution to your problem. If you have any other way to create Random Numbers, please do share it with all the readers using the comments section. Happy Reading.. :&#8211;)<\/em><\/p>\n<div style=\"font-family: Verdana,sans-serif; border: 1px solid #c8b560; padding: 3mm; background: none repeat scroll 0% 0% #fff8c6; text-align: center;\">\n<p>If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.<\/p>\n<form style=\"text-align: center;\" action=\"http:\/\/feedburner.google.com\/fb\/a\/mailverify\" method=\"post\" onsubmit=\"window.open('http:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=automationrepository\/feeds', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true\" target=\"popupwindow\"><input style=\"width: 160px; height: 20px;\" onfocus=\"if (this.value == 'Enter Your Email Address') {this.value = '';}\" onblur=\"if (this.value == '') {this.value = 'Enter Your Email Address';}\" type=\"text\" value=\"Enter Your Email Address\" \/> <input type=\"hidden\" name=\"uri\" value=\"automationrepository\/feeds\" \/> <input type=\"hidden\" name=\"loc\" value=\"en_US\" \/> <input type=\"submit\" value=\"Join Us\" \/><\/p>\n<p style=\"text-align: justify;\">\n<\/form>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Many a times you would come across the need to create Random Numbers in your script. And this random number requirement would come in different flavors &#8211; you might need to create a random number of a certain length, or you might want a random number within a specific range, or you might just need<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[44],"class_list":["post-577","post","type-post","status-publish","format-standard","hentry","category-qtp-basic-stuff","category-qtp-concepts","tag-random-numbers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>3 Different Ways to Create Random Numbers in QTP - XX<\/title>\n<meta name=\"description\" content=\"how to generate random numbers in QTP? See our 3 methods that use Randomize, Rnd and DateTime Stamp methods to create random numbers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anish Pillai\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/\",\"name\":\"3 Different Ways to Create Random Numbers in QTP - XX\",\"isPartOf\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\"},\"datePublished\":\"2011-10-10T20:12:39+00:00\",\"dateModified\":\"2011-10-10T20:15:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\"},\"description\":\"how to generate random numbers in QTP? See our 3 methods that use Randomize, Rnd and DateTime Stamp methods to create random numbers.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.automationrepository.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"3 Different Ways to Create Random Numbers in QTP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/\",\"name\":\"XX\",\"description\":\"\\r\\nasas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\",\"name\":\"Anish Pillai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"caption\":\"Anish Pillai\"},\"description\":\"Find more about Anish Pillai on Google+\",\"sameAs\":[\"http:\/\/www.automationrepository.com\"],\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"3 Different Ways to Create Random Numbers in QTP - XX","description":"how to generate random numbers in QTP? See our 3 methods that use Randomize, Rnd and DateTime Stamp methods to create random numbers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/","twitter_misc":{"Written by":"Anish Pillai","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/","url":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/","name":"3 Different Ways to Create Random Numbers in QTP - XX","isPartOf":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#website"},"datePublished":"2011-10-10T20:12:39+00:00","dateModified":"2011-10-10T20:15:47+00:00","author":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20"},"description":"how to generate random numbers in QTP? See our 3 methods that use Randomize, Rnd and DateTime Stamp methods to create random numbers.","breadcrumb":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.automationrepository.com\/wordpress\/2011\/10\/generate-random-numbers-in-qtp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.automationrepository.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"3 Different Ways to Create Random Numbers in QTP"}]},{"@type":"WebSite","@id":"https:\/\/www.automationrepository.com\/wordpress\/#website","url":"https:\/\/www.automationrepository.com\/wordpress\/","name":"XX","description":"\r\nasas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20","name":"Anish Pillai","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","caption":"Anish Pillai"},"description":"Find more about Anish Pillai on Google+","sameAs":["http:\/\/www.automationrepository.com"],"url":"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/"}]}},"_links":{"self":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/comments?post=577"}],"version-history":[{"count":31,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/577\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/tags?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}