Vugen: Old School Random Selections With srand

At the beginning of your script where you declare variables, seed time() with the srand() function:

char buffer[80];
int TotalNum, RandNum, i;
srand(time(NULL));

Here is how you might use this. Let’s say you need to use a random selection from a web page. This might be a different check box or drop down selection and you want it to be random each time:

//First grab the total count of items to choose from using ORD=ALL
 
web_reg_save_param("pCapture", "LB=something",
 "RB=something else", "Ord=All", LAST);
 
web_submit_data("BLINGBLING",
 "Action=https://heyyaserver/blingbling.asp",
 "Method=POST",
 "RecContentType=text/html",
 "Referer=https://heyyaserver/default.asp",
 "Snapshot=t35.inf",
 "Mode=HTML",
 ITEMDATA,
 "Name=txtAction", "Value=Submit", ENDITEM,
 "Name=homeydontplaydat", s1, ENDITEM, EXTRARES,
 "Url=../images/snoopdogg.jpg", ENDITEM,
 LAST);
 
// First get the total count of what was captured
 
TotalNum = atoi(lr_eval_string("{pCapture_count}"));
 
// Now get a random number between 1 and the total count
 
RandNum = rand() % TotalNum + 1;
sprintf(buffer, "{pCapture_%d}", RandNum);
 
// Now save the new selection as a true parameter in Vugen that can
// be used in this instance.
 
lr_save_string(lr_eval_string(buffer), "pNewAssigned");
 
// What is the value of it? Send a message to the output log
 
lr_output_message("The assigned value is: %s",
lr_eval_string("{pNewAssigned}"));

In more recent versions of LoadRunner, there are easier ways to do this with newer functions. However, I think it is still important to know how to do it other ways. Can you think of other uses for this? If so, let me know in the comments section below.

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Scott Moore

About Scott Moore (153 articles)

With over 20 years of IT experience with various platforms and technologies, Scott has tested some of the largest applications and infrastructures in the world. He is a Certified Instructor and Certified Product Consultant in HP’s LoadRunner and Performance Center products. He currently holds HP certifications for ASE, ASC, and CI. A thought leader in the APM space, he speaks regularly at IT conferences and events



  • http://twitter.com/jcohen0822 Joe Cohen

    You can capture an array of search results and randomly choose a different link to click in the next step of your script. I am “old school” (but I already knew that) because I prefer using a similar method to what you’ve outlined in this post. Good read…thanks.