samedi 13 novembre 2021

Where does the randomness come in this fast random number generator? [duplicate]

I was looking for a fast random number generator and found this one. I made a small change to it by fixing a seed inside the function:

float frand()
{
    static int seed = 42;
    union
    {
        float fres;
        unsigned int ires;
    };

    seed *= 16807;
    ires = ((((unsigned int)seed) >> 9) | 0x3f800000);
    return fres - 1.0f;
}

So I tried it in a loop and indeed it generates a random number between 0 and 1 at every iteration. However in this functions everything seems to be fixed so I don't understand how it generates a different number after each new call.

Also I am not sure why after removing the static keyword before the seed declaration, it generates a random number only ones and then after each new call of the function it returns the same random number.




Aucun commentaire:

Enregistrer un commentaire