- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Good Article which gave me a solution, Thanks Boris.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

Regards,

Mario

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Any other solution for this issue?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Hi,

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

Thanks,

Boris.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Good Article which gave me a solution, Thanks Boris.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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