fastrand icon indicating copy to clipboard operation
fastrand copied to clipboard

add more float generation functions

Open lemire opened this issue 7 months ago • 2 comments

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

lemire avatar May 14 '25 11:05 lemire

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).

jart avatar May 14 '25 21:05 jart

Thanks @jart.

lemire avatar May 15 '25 14:05 lemire