fastrand
fastrand copied to clipboard
add more float generation functions
There's also 1. / 9007199254740991. * (x >> 11) for [0,1] and 1. / 4503599627370496. * ((x >> 12) + .5) for (0,1) interval.
https://x.com/justinetunney/status/1922584035144171817?s=46&t=-zo9kVFDyKuN4X1cdtkIrw
Here's the code I use in cosmopolitan:
/**
* Generates number on [0,1]-real-interval.
*/
double _real1(uint64_t x) {
return 1. / 9007199254740991. * (x >> 11);
}
/**
* Generates number on [0,1)-real-interval.
*/
double _real2(uint64_t x) {
return 1. / 9007199254740992. * (x >> 11);
}
/**
* Generates number on (0,1)-real-interval.
*/
double _real3(uint64_t x) {
return 1. / 4503599627370496. * ((x >> 12) + .5);
}
I checked and _real2() is identical to the double result = (double)(r & 0x1FFFFFFFFFFFFF) * 0x1p-53; expression you use in this project. Except my version uses the higher bits (since that's usually a best practice for multiplicative random number generators).
Thanks @jart.