Pages

Thursday, March 15

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.