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.
sir, plz explain this command and function of
1>& Mid
2> (1, Len(MainStr),1)
myStr=myStr & Mid(MainStr,RandomNumber(1, Len(MainStr)),1)
This comment has been removed by the author.
@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
Wrong program as there is no Random function as Rnd there which does not take any argument
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