News for hackers. Stuff that matters.
Re:There's Math.random(), and then there's Math.random() (2015) by fluoridation on Wednesday November 08, 2023 @01:01PM (#38194314)
You are correct, however picking floats uniformly like that is much more difficult, as some values should be thousands of orders of magnitude more likely to be picked than others. It's much easier to implement a generator that randomizes the mantissa and calls it a return.
Re:There's Math.random(), and then there's Math.random() (2015) by fanf2 on Wednesday November 08, 2023 @01:09PM (#38194412)
My favourite implementation is

    double random_double(rng_t *rng) {
        return((double)(random_uint64(rng) >> 11) * 0x1.0p-53);
    }
It has the advantage of not needing bit_cast (which C lacks) and has 53 instead of 52 bits of randomness.
Re:There's Math.random(), and then there's Math.random() (2015) by smegsicle on Wednesday November 08, 2023 @01:14PM (#38194486)
i for one expect my random floating point numbers to have the same distribution as a random bitfield interpreted as a float, and anything else should be considered a bug
Re:There's Math.random(), and then there's Math.random() (2015) by fanf2 on Wednesday November 08, 2023 @01:20PM (#38194600)
I don’t think that would be very useful, because there would be a 1/2048 chance of getting a NaN, and it would be difficult to convert the numbers to a known distribution.