Deep Patel: QTP Random Number Generation - RandomNumber.Value vs Rnd and Randomize

Monday, February 27, 2012

QTP Random Number Generation - RandomNumber.Value vs Rnd and Randomize

Recently I was facing a problem in generating Random numbers using QTP.

I started with QTP's inbuilt RandomNumber.Value(upperlimit,lowerlimit)

and the result was:

It would generate random numbers for that instance of QTP. But once QTP is restarted it will generate same numbers in same sequence. Example is shown in the images below.

The QTP always starts the Random Number Generation from the same number. To elaborate, QTP's Random Number function gets initialized with the same number(the seed) every time QTP's instance is created.

So for better performance we can use vb's Rnd function with call to Randomize. Call to Randomize initializes the Rnd function with a new seed value every time(this is based on the system time).
If Randomize is not used, Rnd uses the same number as a seed the first time it is called.
Hence it will generate the same value everytime the function is called.


Below are the results of all the cases. The execution 1 and execution 2 are of separate QTP instances.

Using QTP’s RandomNumber.Value(a,b) function.



Using vb’s Rnd function with Randomize.
This will generate random numbers every time!


Using vb’s Rnd function without Randomize.


Wondering what would happen on using Randomize with QTP's RandomNumber.Value(a,b) !!
Here are the results...

For more on Rnd Function follow check this link


For more on Randomize Function follow check this link

No comments:

Post a Comment