random numbers & words
The random function is extremely useful, no matter the language or application. I personally utilise it a lot, between many different languages. I have found, however, that it is one of those functions I always seem to forget how to do, and find myself re-learning it again.
I find the issue when I change from, be it, ruby, to qtp to loadrunner to winrunner. do while loops, counters, and arrays are all quite standard, but random, well, it gets me every time.
Although I find loadrunner’s flavor of C to be a bit of a bugbear, it is, not all that bad to work with. Of course after working with ruby for so long, you get a little spoilt with the ability to convert 10 lines of code to just 2.
and the random function is no different. Ruby does it beautifully, loadrunner…not so.
Sure you can use a parameter file to do this, quite easily actually, but what if you
random number in load runner
example 1:
Lets say I want a random number between 2 numbers. In this instance I need to randomly generate the last 4 digits of a phone number, the number must be between 5000 and 9000 (and I don’t want to use a laodrunner parameter list — just to be difficult)
//declare int int rNum; //srand Returns a pseudo-random integer in the range 0 at least 32767 (RAND_MAX) srand(time(NULL)); rNum = rand() % 4000; //rNum is now a number between 0-4000 rNum += 5000; //rNum is now a number between 5000-9000 //Save to a load runner parameter. lr_save_int(rNum,"phoneNum");
example 2:
Using 2 parameters to generate a random between them. In this example I have saved two strings, numberA and numberB. This is just for the example, in the real life scenario, I got these two values from a database, and they were always different, hence the need to write a function to generate the random number – also woth noting, you cant do this using loadrunner’s parameter list.
int random; int inbetween; char temp[4]; lr_save_string("9000",numberA); lr_save_string("5000",numberB); srand(time(NULL)); //convert the character to integeter (atoi) and assign the integer to parameter //'inbetween' which is paramter 'numberA' minus 'numberB' inbetween = atoi(lr_eval_string("{numberA}")) - atoi(lr_eval_string("{numberB}")); lr_output_message("%d" , inbetween); //inbetween is now equal to 9000-5000 which = 4000 random = rand() % inbetween; //random is now a number between 0 and 4000 random+= atoi(lr_eval_string("{numberB}")); lr_output_message("%d" , random); //random is now a integer between 5000 and 4000 //convert the int back to a string, assign it to temporary C paramter 'temp' itoa(random,temp,10); //save the string assigned to 'temp' to loadrunner parameter 'randomNumber' lr_save_string(temp,"randomNumber");
random number in ruby
it doesn’t get any easier than this.
#assign @phoneNum to an value between 5000 and 9000 @phoneNum = rand(5000) + 4000
random number in qtp
This will give you a random number, length of 4, between 0 an 9.
result = "" for var_i = 1 to 4 result = Result & RandomNumber(0,9) next
random word in loadrunner
This is a function to be used in loadrunner. Place it in the end of vuser_init or create a functions.h file. Instructoins to do are here.
random_alpha(char* param_name, int length) { char buff[32] = ""; int r,i; char c; srand((unsigned int)time(0)); //Seed number for rand() for (i = 0; i < length; i++) { // A-Z = 65-90 = rand() % 25 + 65 r = rand() % 25 + 65; c = (char)r; buff[i] = c; printf("%c", c); } lr_save_string(buff, param_name); return 0; }
invoke the function
//calls the random_alpha function, //assigns it to parameter 'randomName' of 6 characters. random_alpha("randomName",6);
random word in ruby
ef random_word letters = { ?v => 'AEIOU', ?c => 'BCDFGHJKLMNPRSTVWYZ' } word = '' 'cvcvcv'.each_byte do |x| source = letters[x] word << source[rand(source.length)].chr end return word end puts random_word
random word in qtp
Function RandomString( ByVal strLen ) Dim str Const LETTERS = "abcdefghijklmnopqrstuvwxyz0123456789" For i = 1 to strLen str = str & Mid( LETTERS, RandomNumber( 1, Len( LETTERS ) ), 1 ) Next RandomString = str End Function
Enjoy!