vendredi 1 septembre 2017

Generate uniformly distributed random number in Nginx?

Nginx seems to have a built-in function called ngx_random that's used in various places of the source code. But it seems just to be defined as:#define ngx_random random

If I'm understanding this correctly, that means all the places Nginx calling ngx_random() it's just calling (on Linux platform) random(). From the doc it isn't clear to me that it is guaranteed uniform distribution with a given range in any way, and I'm suspecting that similar to rand(), it's not uniform at all, and will only be uniform if range n is divisible by RAND_MAX.

But the nice thing of using ngx_random is I believe the system takes care of the seeding automatically, during startup time. Whereas if I want to use something truly uniform with my range, like drand48, I have no idea how I'm gonna seed it?

So what should I do about it? Is my assumption on ngx_random correct? And if I want to use drand48 in any place of the various modules, how can I seed it (obviously I don't want to seed every time before using it)?




Aucun commentaire:

Enregistrer un commentaire