Skip to content

Instantly share code, notes, and snippets.

@kevinmoran
Created March 31, 2021 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmoran/9a0af144f49c32a441cd51bb520fee96 to your computer and use it in GitHub Desktop.
Save kevinmoran/9a0af144f49c32a441cd51bb520fee96 to your computer and use it in GitHub Desktop.
Converting an integer to a float between 0 and 1 with bithacks (for PRNG)
// Source: "xoshiro / xoroshiro generators and the PRNG shootout"
// https://prng.di.unimi.it/
#include <stdint.h>
static inline double u64ToDoubleBetween01(uint64_t x) {
return (x >> 11) * 0x1.0p-53;
}
// I came up with the following function for 32-bit floats based on the above, let me know if it's wrong
static inline float u32ToFloatBetween01(uint32_t x) {
return (x >> 8) * 0x1.0p-24f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment