|
From: | Daniel J Sebald |
Subject: | Re: Problems with Octave random number generation, part 1 |
Date: | Wed, 18 Dec 2019 02:16:02 -0500 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 |
On 12/18/19 12:39 AM, Daniel J Sebald wrote:
On 12/17/19 2:27 PM, Rik wrote:Maintainers,
[snip]
Given that IEEE single-precision floating point has only 24 bits for the mantissa, I think something like this would work better static float randu32 (void) { uint32_t i; do { i = randi32 () & static_cast<uint32_t> (0xFFFFFF); } while (i == 0 || i == 0xFFFFFF); return i * (1.0f / 16777216.0f); } In my testing, this does not produce exact values of 1, nor does it mapmultiple integer values to the same floating point value. Is anyone awareof problems with this algorithm?Creative, but mapping out that whole upper level of pseudo-random numbers doesn't feel good. It may be as good as anything though because, yeah, we could change the name of the function to randu24() and it would have the same meaning because of the imposed range of (0,1). In some way, that range must implicitly toss away the exponent and the floating point bit. It's effectively now fractional point, not floating point.
In fact, the range [1:0xFFFFFE] could go directly into the mantissa rather than multiply or divide. I.e., think of 1/16777216 as 2^-24 or right bit shift by 24. Is there a C macro for loading a mantissa directly?
Dan
[Prev in Thread] | Current Thread | [Next in Thread] |