Highlighted
Super Contributor.
Super Contributor.
8623 views

Math.random() equalent function

Jump to solution

Hello,

 

is there any euqalent function in C to Math.random() function in JavaScript?

 

Math.random()--output in JavaScript-->0.047033395492444674 (which will generate a random number between 0<=rand<1).

 

Any c-function or loadrunner function available which will give similar type of random number? pls suggest.

 

Thanks in advance.

 

Ajay

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Super Contributor.
Super Contributor.

Re: Math.random() equalent function

Jump to solution

Good Article which gave me a solution, Thanks Boris.

View solution in original post

0 Likes
20 Replies
Highlighted
Absent Member.
Absent Member.

Re: Math.random() equalent function

Jump to solution

This is from the function reference in Vugen. You can check that for all functions available in Vugen.

 

 

Gets a random integer between 0 and RAND_MAX.

int rand( void );

 

Regards,

Mario

HP LR/PC Support

If you are customer you can try posting on our internal forums for more personalized support.

LoadRunner:
http://h30499.www3.hp.com/t5/LoadRunner-Support-Customer/bd-p/loadrunner-support-customer-forum

Performance Center:
http://h30499.www3.hp.com/t5/Performance-Center-Support/bd-p/performance-center-support-customer

How to:
http://www.youtube.com/watch?v=4ChlysWupWM

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
Highlighted
Super Contributor.
Super Contributor.

Re: Math.random() equalent function

Jump to solution

int rand( void ); function will return a integer number between 0 and RAND_MAX,

but in this case the number need to be a float number like (Math.random()--output in JavaScript-->0.047033395492444674 (which will generate a random number between 0<=rand<1).

 

so we need to search for other options available.

 

Thanks, Ajay

0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Math.random() equalent function

Jump to solution

No vugen function would be available to do something like that.

 

Regards,

Mario

HP LR/PC Support

If you are customer you can try posting on our internal forums for more personalized support.

LoadRunner:
http://h30499.www3.hp.com/t5/LoadRunner-Support-Customer/bd-p/loadrunner-support-customer-forum

Performance Center:
http://h30499.www3.hp.com/t5/Performance-Center-Support/bd-p/performance-center-support-customer

How to:
http://www.youtube.com/watch?v=4ChlysWupWM

If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
0 Likes
Highlighted
Super Contributor.
Super Contributor.

Re: Math.random() equalent function

Jump to solution

Thanks for the inputs!

 

We have a scenario where we need to pass the Math.random() (which is a javascript function) number in the request.

 

in that case what are the options/solutions we have to re-produce/resolve the similar type of random number issues using c/loadrunner?

 

Thanks, Ajay

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Math.random() equalent function

Jump to solution

Hi

 

I just posted this and did not appear. Apologies if it ends up here twice:

 

This is a quick and dirty workaround, pending a real solution. You can play with the output format.

Remember to seed you random number generator with srand(time(NULL)) or similar.

 

    // ***********************
    // Declare local variables
    // ***********************
    int rc;
    double rand_num;
    // ***********************

    do
    {
        rc = rand();

        if(rc != 0)
        {
            rand_num = 1.00000000 / rc;
        }
    }
    while(rc == 0);

    lr_output_message("rand is %d rand_num is: %16.14f", rc, rand_num);


Highlighted
Super Contributor.
Super Contributor.

Re: Math.random() equalent function

Jump to solution

Any other solution for this issue?

0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Math.random() equalent function

Jump to solution

Hi,

 

You can just use the one from JavaScript, see my blog post to learn how:

http://h30499.www3.hp.com/t5/HP-LoadRunner-and-Performance/How-to-use-JavaScript-in-your-HP-LoadRunner-scripts/ba-p/6197321#.UoSv_hrdeAk

 

Thanks,

Boris.

Highlighted
Super Contributor.
Super Contributor.

Re: Math.random() equalent function

Jump to solution

Good Article which gave me a solution, Thanks Boris.

View solution in original post

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Math.random() equalent function

Jump to solution

Hi Ajay,

 

Can you ples share the code for the same.I am testing hyperion reporting application where it generates rnd=0.11877714585533722  and I see this value only on request,if I look at the generation log i can see as URL += "&rnd=" + Math.random();.

 

Also I see a behavior like after 15 iteration id  using the same hardcoded value work fine but then it fails.

 

If you can share the code ,I will complete my task as execution is tommorrow for me.

 

Thanks,

Akshat

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Math.random() equalent function

Jump to solution

Calling srand() is quite important, but supplying it with a quality seed is even more important.

ylib has a wrapper for rand() that if not initialized will automatically generate a random seed based on vuser id, group, current time (in milliseconds), and an iterator to guarantee all virtual users will get their own unique seed.
Furthermore, it extends MAX_RAND (the highest generated number) from 32768 (15 bits) to 2147483648 (31 bits).

If you use that function this workaround can be improved to something generic:

double js_math_random()
{
    return 1 / y_rand();
}

Which we can then wrap with something that saves it to whatever parameter you want:

void js_math_random_to_param(char *paramname)
{
    lr_param_sprintf(paramname, "%f",  js_math_random());
}

.. which you can then call inside your script:

js_math_random_to_param("random_parameter");

---
Discover the y-lib loadrunner support library:
https://github.com/randakar/y-lib
0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Math.random() equalent function

Jump to solution

Can have ur number or do I contact you so that I can show my scenario to you.Please

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.