Quantcast
Viewing all articles
Browse latest Browse all 12134

Re: Math.random() equalent function

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


Viewing all articles
Browse latest Browse all 12134

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>