LR 11.51: How do I generate a better random number
Question ID: 104870
2
0

I have noticed that even though I’m using a random integer parameter in my scripts, the pattern of random numbers remains the same between different executions of the same script. Anybody know how to handle this?

Marked as spam
Posted by (Questions: 23, Answers: 4)
Asked on February 28, 2013 8:29 pm
829 views
Answers (2)
3
Private answer

Random numbers on a computer are actually pseudo-random number. Given the same random seed, the paterern of random values returned between executions will remain the same. The solution to this is to set the random number generator with a new seed value everytime you run the script. This code will help to give a a better patern of random numbers.

Include this code in your vuser_init:

vuser_init()
{
/*** Seed the random number generator once per script with the current time ***/
/*** If this is not done, the same numbers may be generated ***/
srand ( time(NULL) );
return 0;
}

Then add this subroutine to your script in globals.h:

/*** Function - GetRandomNumber ***/
int GetRandomNum (int MaxNum)
{
int RandNum = 0;
/*** Use the rand function to get a Random number from 0 to MaxNum ***/
RandNum = (int)((rand()%MaxNum)+1);
return RandNum;
}

Then call the subroutine from your action section whenever you need a random number

actions()
{
int RandomNumber, i; //Receives the value from the random routine
int MaxNum =32000; //Set the upper limit of the random numbers
/*** Get ten random values from 0 to 200 ***/
for( i = 1; i < 10; i++) { RandomNumber = GetRandomNum(MaxNum); //This is the call to the function lr_output_message(''Random Number = %d'', RandomNumber); //Display a message } return 0; }

Marked as spam
Posted by (Questions: 4, Answers: 41)
Answered on February 28, 2013 8:42 pm
0
Private answer

In one of script requires random decimal number between 0 to 100.(example 96.42).How to generate random decimal numbers?

Marked as spam
Posted by (Questions: 0, Answers: 1)
Answered on June 12, 2017 5:50 pm
EyeOnTesting

Welcome back to "EyeOnTesting" brought to you by Orasi Software, Inc.

X
Scroll to Top