summaryrefslogtreecommitdiff
path: root/numpy/random/src/distributions
diff options
context:
space:
mode:
authorwarren <warren.weckesser@gmail.com>2021-11-06 02:59:05 -0400
committerwarren <warren.weckesser@gmail.com>2021-11-06 03:20:51 -0400
commit4b9e56921e6343b980b27e308534ca433a4a1fe8 (patch)
treebfaf0b87c2dad22890b9629df7a07454b3560ba5 /numpy/random/src/distributions
parent7c211013d559db238daf76fdb90af1d2f37a341e (diff)
downloadnumpy-4b9e56921e6343b980b27e308534ca433a4a1fe8.tar.gz
BUG: Get full precision for 32 bit floating point random values.
The formula to convert a 32 bit random integer to a random float32, (next_uint32(bitgen_state) >> 9) * (1.0f / 8388608.0f) shifts by one bit too many, resulting in uniform float32 samples always having a 0 in the least significant bit. The formula is corrected to (next_uint32(bitgen_state) >> 8) * (1.0f / 16777216.0f) Occurrences of the incorrect formula in numpy/random/tests/test_direct.py were also corrected. Closes gh-17478.
Diffstat (limited to 'numpy/random/src/distributions')
-rw-r--r--numpy/random/src/distributions/distributions.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/numpy/random/src/distributions/distributions.c b/numpy/random/src/distributions/distributions.c
index adf4db4a7..bd1e1faa4 100644
--- a/numpy/random/src/distributions/distributions.c
+++ b/numpy/random/src/distributions/distributions.c
@@ -17,7 +17,7 @@ static NPY_INLINE uint64_t next_uint64(bitgen_t *bitgen_state) {
}
static NPY_INLINE float next_float(bitgen_t *bitgen_state) {
- return (next_uint32(bitgen_state) >> 9) * (1.0f / 8388608.0f);
+ return (next_uint32(bitgen_state) >> 8) * (1.0f / 16777216.0f);
}
/* Random generators for external use */