Generate a random string in QTP/vbscript

Hello Friends,

Sometime script/application requires some input data as string which is unique. Random strings is helpful is this scenario. Lets see how to generate random input string in qtp.

Function GenerateRandomString(StrLen)
Dim myStr
Const MainStr= “0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz”
For i = 1 to StrLen
  myStr=myStr & Mid(MainStr,RandomNumber(1, Len(MainStr)),1)
Next
GenerateRandomString = myStr
End Function

Here StrLen(argument) is the required length of the string. Call this function as below-

MsgBox GenerateRandomStrin(6)

It will generate a string of 6 characters.

In case of any queries, please post your comment.

5 thoughts on “Generate a random string in QTP/vbscript”

  1. sir, plz explain this command and function of
    1>& Mid
    2> (1, Len(MainStr),1)
    myStr=myStr & Mid(MainStr,RandomNumber(1, Len(MainStr)),1)

  2. @Aryan

    1. & is used for concatenation of strings
    2. Mid(MainStr,RandomNumber(1, Len(MainStr)),1)

    in above line, mid function takes 3 arguments

    1. MainStr
    2. RandomNumber(1, Len(MainStr)) – result of this line will be passed as 2nd argument to Mid function

    3. 1

  3. Use Below to generate a random number
    Dim max,min
    max=100
    min=1
    Randomize

    for i=1 to 100

    msgbox (Int((max-min+1)*rnd+min))

    next

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top