I'm looking for an efficient way to generate random floating-point numbers on the open-open interval (0,1). I currently have an RNG that generates random integers on the closed-closed interval of [0, (2^32)-1]. I've already created a half-open floating point RNG on the interval [0,1) by simply multiplying my result from the integer RNG by 1/((2^32)-1) rather than dividing by (2^32)-1 since it's inefficient.
The way I'm currently going about generating numbers on the interval (0,1) is with a conditional statement like the one below:
float open_open_flt = (closed_open_flt==0) ? closed_open_flt : FLT_MIN;
Unfortunately, this is rather inefficient since it is control code and I feel like it introduces some bias.
Can anybody suggest an alternative?
The Math.random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range.
To generate “true” random numbers, random number generators gather “entropy,” or seemingly random data from the physical world around them. For random numbers that don't really need to be random, they may just use an algorithm and a seed value.
The random() method returns a random floating number between 0 and 1.
Using the rand() function to generate random number between 0 and 1 in C++ The rand() function is used to generate a random number in C++.
You are already there.
The smallest distance between two floats your current generator produces is 1/(2^32).
So, your generator is efectively producing [0,1-1/(2^32)].
1/(2^32) is greater than FLT_MIN.
Thus if you add FLT_MIN to your generator,
float open_open_flt = FLT_MIN + closed_open_flt;
you'll get [FLT_MIN,1-(1/(2^32))+FLT_MIN], which works as a (0,1) generator.
Since the probability of actually observing 0 probability is very small, and checking if a number is equal to 0 is least expensive (as compared to addition or multiplication), I would regenerate the random number repeatedly until it is not equal to 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With