Re: Math.random() equalent function (988 Views)
Reply
Valued Contributor
Ajay_U
Posts: 108
Registered: ‎04-30-2012
Message 1 of 14 (1,214 Views)
Accepted Solution

Math.random() equalent function

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

HP Expert
Suerte13cr
Posts: 736
Registered: ‎09-14-2011
Message 2 of 14 (1,211 Views)

Re: Math.random() equalent function

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.
Valued Contributor
Ajay_U
Posts: 108
Registered: ‎04-30-2012
Message 3 of 14 (1,205 Views)

Re: Math.random() equalent function

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

HP Expert
Suerte13cr
Posts: 736
Registered: ‎09-14-2011
Message 4 of 14 (1,197 Views)

Re: Math.random() equalent function

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.
Valued Contributor
Ajay_U
Posts: 108
Registered: ‎04-30-2012
Message 5 of 14 (1,195 Views)

Re: Math.random() equalent function

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

Frequent Advisor
DaveP_1
Posts: 74
Registered: ‎08-19-2010
Message 6 of 14 (1,176 Views)

Re: Math.random() equalent function

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);


Valued Contributor
Ajay_U
Posts: 108
Registered: ‎04-30-2012
Message 7 of 14 (995 Views)

Re: Math.random() equalent function

Any other solution for this issue?

HP Expert
_Buzzy
Posts: 105
Registered: ‎08-10-2011
Message 8 of 14 (988 Views)

Re: Math.random() equalent function

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-LoadRunn...

 

Thanks,

Boris.

Valued Contributor
Ajay_U
Posts: 108
Registered: ‎04-30-2012
Message 9 of 14 (978 Views)

Re: Math.random() equalent function

Good Article which gave me a solution, Thanks Boris.

Occasional Advisor
thick client testing
Posts: 20
Registered: ‎11-15-2009
Message 10 of 14 (970 Views)

Re: Math.random() equalent function

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

Advisor
randakar
Posts: 15
Registered: ‎11-22-2013
Message 11 of 14 (942 Views)

Re: Math.random() equalent function

[ Edited ]

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
Occasional Advisor
thick client testing
Posts: 20
Registered: ‎11-15-2009
Message 12 of 14 (849 Views)

Re: Math.random() equalent function

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

Valued Contributor
Ajay_U
Posts: 108
Registered: ‎04-30-2012
Message 13 of 14 (837 Views)

Re: Math.random() equalent function

Attached sample script for Math.random() function, before running the script make sure that "Enable running Javascript code" to "YES". 

 

Let me know if you need any other information.

 

Thanks, Ajay

Advisor
randakar
Posts: 15
Registered: ‎11-22-2013
Message 14 of 14 (829 Views)

Re: Math.random() equalent function

[ Edited ]

 

While running javascript code from within vugen is nice, is it really necessary here to set up an entire javascript execution engine just for a single call to a commonly used C library function?

 

Think about it - how is this going to perform in the real world, compared to the two invocations of rand() done by the y_rand() function?

 

( https://github.com/randakar/y-lib/blob/master/y_core.c#L144 is where that lives at right now, btw. )

 

 

---
Discover the y-lib loadrunner support library:
https://github.com/randakar/y-lib
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.