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.

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

ReplyDelete1>& Mid

2> (1, Len(MainStr),1)

myStr=myStr & Mid(MainStr,RandomNumber(1, Len(MainStr)),1)

This comment has been removed by the author.

ReplyDelete@Aryan

ReplyDelete1. & 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

ReplyDeleteUse Below to generate a random number

ReplyDeleteDim max,min

max=100

min=1

Randomize

for i=1 to 100

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

next

Hi abhikansh,

ReplyDeleteSometimes, it is generating a random number like "="47"

When I give input as 3 digits

Please check this issue ...

Hello,

ReplyDeleteI had a Question.

Suppose consider in the datatable in 1 column we are having some "n" number of rows and from that n no. of rows I want to generate 10 random rows with that data in particular row.

How to generate it??

Its urgent can you get it for me.

Regards,

Shanmuk

It should generate by using Random function.

ReplyDeleteThanks for your post 'Abhikansh Jain'!

ReplyDeleteHi

ReplyDeleteI have a query pls solve this at the earliest

i have a data in notepad like

one has spent rs 100 from his salary rs 10000

two has spent rs 150 from his salary account rs 13000

i want output to be printed like

rs 10000

rs 13000

str = "abcdefghijklmnopqrstuvwxyz"

ReplyDeleteFor i = 1 To 8 Step 1

Randomize

x = Int((26*Rnd)+1)

char = Mid(str,x,1)

str1 = str1&char

Next

MsgBox str1