Re: Math.random() equalent function (1464 Views)
Reply
Valued Contributor
Posts: 117
Registered: ‎04-30-2012
Message 1 of 20 (1,690 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
Posts: 736
Registered: ‎09-14-2011
Message 2 of 20 (1,687 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
Posts: 117
Registered: ‎04-30-2012
Message 3 of 20 (1,681 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
Posts: 736
Registered: ‎09-14-2011
Message 4 of 20 (1,673 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
Posts: 117
Registered: ‎04-30-2012
Message 5 of 20 (1,671 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
Posts: 75
Registered: ‎08-19-2010
Message 6 of 20 (1,652 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
Posts: 117
Registered: ‎04-30-2012
Message 7 of 20 (1,471 Views)

Re: Math.random() equalent function

Any other solution for this issue?

HP Expert
Posts: 105
Registered: ‎08-10-2011
Message 8 of 20 (1,464 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
Posts: 117
Registered: ‎04-30-2012
Message 9 of 20 (1,454 Views)

Re: Math.random() equalent function

Good Article which gave me a solution, Thanks Boris.

Occasional Advisor
Posts: 20
Registered: ‎11-15-2009
Message 10 of 20 (1,446 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
Posts: 18
Registered: ‎11-22-2013
Message 11 of 20 (1,418 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
Posts: 20
Registered: ‎11-15-2009
Message 12 of 20 (1,325 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
Posts: 117
Registered: ‎04-30-2012
Message 13 of 20 (1,313 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
Posts: 18
Registered: ‎11-22-2013
Message 14 of 20 (1,305 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
Valued Contributor
Posts: 117
Registered: ‎04-30-2012
Message 15 of 20 (367 Views)

Re: Math.random() equalent function

It's just one instance where we need to call simple java script function, but now a days most of the applications using client side javascript to generate some dynamic values and to generate the same values from LR this feature would be very useful. And more over it won't execute the entire downloaded javascript it will just execute the code block which we explicitly call.

 

I don't think re-writing the entire Javascript code to C would be a good and feasible solution.

 

Thanks, Ajay

Advisor
Posts: 18
Registered: ‎11-22-2013
Message 16 of 20 (363 Views)

Re: Math.random() equalent function

You make it sounds like doing this is terribly complicated. Which it's not. You just need a random number. It's not rocket science. Far from it.

---
Discover the y-lib loadrunner support library:
https://github.com/randakar/y-lib
Valued Contributor
Posts: 117
Registered: ‎04-30-2012
Message 17 of 20 (345 Views)

Re: Math.random() equalent function

It's not a rocket science, but if it's a very simple function to implement in C then it would be very easy and no need to go for javascript, but i came across many cases where you need to execute bunch of Javascript code to generate one Authrization code/string, it would be very difficult and time consuming activity to understand all that javascript code and again converting that to C. In such cases Enabling Javascript execution is the best option.

 

Thanks, Ajay

Advisor
Posts: 18
Registered: ‎11-22-2013
Message 18 of 20 (328 Views)

Re: Math.random() equalent function

Instead of trying to understand the code I just try to understand the string in question. That usually is much easier than reading possibly obfuscated js code.
Usually if a code is generated it's just a timestamp, some (user) ids, and possibly a random number concatenated together somehow. Figuring that out doesn't necessarily have to be difficult at all. Especially since the dev team writing the code should be close at hand anyway and capable of answering questions like "how is this field generated?"
---
Discover the y-lib loadrunner support library:
https://github.com/randakar/y-lib
Valued Contributor
Posts: 117
Registered: ‎04-30-2012
Message 19 of 20 (316 Views)

Re: Math.random() equalent function

Yeah True, if it's simple timestamp (or) any other easy string we can easily do that in C, in my case i have a situation where we need to pass the current URL, Method(GET ,POST, DELETE), Post Data, Query String and current timestamp as arguments to a Javascript function, which is a propriatary encryption algoritham (About 150 lines of java script code) to generate the Encryption key. And we need to pass this key along with our request. In this case instead of spending time on converting this algoritham we can directly use the same Javascript code in LR which is more appropriate and time saver.

 

Note: If we pass any other random string of same lenth it won't accept other than the exact encrypted key.

 

Thanks, Ajay

Advisor
Posts: 18
Registered: ‎11-22-2013
Message 20 of 20 (313 Views)

Re: Math.random() equalent function

 

That makes me laugh. This type of "encryption" is not encryption at all. The user has all the keys so what exactly are you protecting?

 

Ah well, in this case they are purposely trying to make your life hard and aren't even willing to tell you how they do it.

Fine, in this particular case you're right in doing it that way. Life's too short to waste on such a lost cause. ;-)

 

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