> I'm having trouble generating uniform random doubles in [0,1) with
> arc4random. In games, the snippet:
> 
>       (double) arc4random() / (UINT32_MAX + 1.0)
> 
> crops up multiple times, but that isn't utilising the full precision of
> a double. If you do the equivalent with a 64bit random integer:
> 
>       (double) ((uint64_t) arc4random() << 32 | arc4random()) / (UINT64_MAX + 
> 1.0)
> 
> then I can still see two issues. Firstly, we are now putting too much
> precision into a double which is going to add slight bias when it
> rounds. Secondly, `UINT64_MAX + 1.0` probably isn't going to give us
> what we want because of floating point precision.

arc4random is a base2 supplier.  Floating point random systems are
quite a different beast.  You could try something like

       unsigned short rseed[3];

        arc4random_buf(rseed, sizeof rseed);
        return ldexp((double) rseed[0], -48) +
               ldexp((double) rseed[1], -32) +
               ldexp((double) rseed[2], -16);

Reply via email to