Skip to content

Instantly share code, notes, and snippets.

@ogrisel
Last active October 28, 2022 13:54
Show Gist options
  • Save ogrisel/eb3073db349cdf6445f9631f4fc416d5 to your computer and use it in GitHub Desktop.
Save ogrisel/eb3073db349cdf6445f9631f4fc416d5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@smason
Copy link

smason commented Oct 24, 2022

not sure if it makes much difference, but I think your "smarter" version is preferred these days, i.e.:

(uint32_values >> 8).astype(np.float32) * np.float32(1 / (1 << 24))

the bottom of https://prng.di.unimi.it/ has some comments on some different definitions of uniformity that could apply here

I noticed that your twitter message said you wanted $[0..1]$, so you might want a -1 in there. Operator precedence is a bit annoying so it needs more brackets than you might expect:

(uint32_values >> 8).astype(np.float32) * np.float32(1 / ((1 << 24) - 1))

e.g. test with:

uint32_values = np.uint32(-1)

@ogrisel
Copy link
Author

ogrisel commented Oct 28, 2022

Thanks @smason!

@fcharras
Copy link

fcharras commented Oct 28, 2022

float32(np.uint32(x >> uint32(32)) >> uint32(8)) * (float32(1) / float32(uint32(1) << uint32(24))) seems to be better, it seems equivalent to casting an intermediate float64 to float32, without actually materializing the float64 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment