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 – 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 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. :–)

### 1. Create a Random Number having a Fixed Length

I have created a VBScript function that should help you find a random number of any length you want.

'===============================================

'Function to Create a Random Number of Any Length

'===============================================

Function fnRandomNumber(LengthOfRandomNumber)

Dim sMaxVal : sMaxVal = ""

Dim iLength : iLength = LengthOfRandomNumber

'Find the maximum value for the given number of digits

For iL = 1 to iLength

sMaxVal = sMaxVal & "9"

Next

sMaxVal = Int(sMaxVal)

'Find Random Value

Randomize

iTmp = Int((sMaxVal * Rnd) + 1)

'Add Trailing Zeros if required

iLen = Len(iTmp)

fnRandomNumber = iTmp * (10 ^(iLength - iLen))

End Function

'================== End Function =================

To 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 **myRandomNum = fnRandomNumber(6)**

### 2. Create a Random Number Within a Specific Range

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.

'===========================================================

'Function to Create a Random Number within a specified range

'===========================================================

Function fnRandomNumberWithinRange(MinimumRange, `MaximumRange`

)

Dim iMax : iMax = MaximumRange

Dim iMin : iMin = MinimumRange

'Create Random Number within the Range

Randomize

fnRandomNumberWithinRange = Int(((iMax - iMin) * Rnd) + iMin)

End Function

'======================== End Function =====================

Calling the above function as **iRandomNum =Â fnRandomNumberWithinRange(1200, 1500)** would return a random number between 1200 & 1500.

### 3. Create a Random Number with DateTime Stamp

This method doesn’t use the Randomize & Rnd functions to create a random number. Instead it uses the Date & Time combination to come up with a unique number. 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 & time is always unique.

'===========================================================

'Function to Create a Random Number with DateTime Stamp

'===========================================================

Function fnRandomNumberWithDateTimeStamp()

'Find out the current date and time

Dim sDate : sDate = Day(Now)

Dim sMonth : sMonth = Month(Now)

Dim sYear : sYear = Year(Now)

Dim sHour : sHour = Hour(Now)

Dim sMinute : sMinute = Minute(Now)

Dim sSecond : sSecond = Second(Now)

'Create Random Number

fnRandomNumberWithDateTimeStamp = Int(sDate & sMonth & sYear & sHour & sMinute & sSecond)

End Function

'======================== End Function =====================

*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.. :–)*

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

Pingback: Common string manipulation fuinctions that can be used in QTP - Automation Repository()