Generate uniformly random float which can return all possible values

golang random number in range
golang rand uint64
python random float in range
golang rand float64
rand int
rand seed
golang rand shuffle
golang normal distribution

An easy way to generate a random float64 in [0,1) is by generating a uniformly random int in [0,2⁵³) and dividing it by 2⁵³. This is essentially what rand.Float64() is doing. However, not all possible float64 values between 0 and 1 can be generated this way: if the value is lower than 2⁻⁴ for example, the 4 last bits of the significand are always going to be 0. Or, put more simply, the naive method always returns multiples of 2⁻⁵³, and not all floating point numbers between 0 and 1 are multiples of 2⁻⁵³.

How do you generate a uniformly random float64 such as every possible value has a chance of being returned? (Here, uniformly random means over the real interval [0,1): conceptually, I want to pick a uniformly random real number between 0 and 1 and return the closest float.)

For context, I need this because I'm implementing this paper and the assumption "all possible values between 0 and 1 are represented" is essential for the result to hold.


Porting this code (suggested in Severin's answer) is a possible option.

I think that it is equivalent to first generate the significand bits (by generating a random float in [1,2)), and then choose the exponent from a geometric distribution (it has a 0.5 chance of being -1, 0.25 of being -2, etc.).

// uniform returns a uniformly random float in [0,1).
func uniform() float64 {
  sig := rand.Uint64() % (1 << 52)
  return (1 + float64(i)/(1<<52)) / math.Pow(2, geometric())
}

// geometric returns a number picked from a geometric
// distribution of parameter 0.5.
func geometric() float64 {
  b := 1
  for rand.Uint64()%2 == 0 {
     b++
  }
  return b
}

We can probably make geometric() faster by using one of the LeadingZeros* functions from the bits package instead of doing one coin flip per bit.

Python Get Random Float Numbers using random and Uniform , How do you generate a uniformly random float64 such as every possible value has a chance of being returned? (Here, uniformly random  random.uniform () to get a random float number within a range. The random.uniform() function returns a random floating-point number between a given range in Python. Let’s assume you want to generate a random float number between 10 to 100 Or from 50.50 to 75.5. In such cases, you should use random.uniform() function.


Well, standard way, I believe, is to generate up to 1074bits integer and map it to the double. Beware, that your RNG should have internal state at least 1074bits long.

Reference implementation: http://xoshiro.di.unimi.it/random_real.c

Discussion about it: http://xoshiro.di.unimi.it/

Another good link: https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/

9.6. random — Generate pseudo-random numbers, Use random() and uniform() functions to generate a random float number In this article, I will show you how to generate a random float number in Python. The step argument is not available in random.uniform() float number from a range() with a specific interval (The step value)? Show All Exercises. So one strategy we can take is to generate a random bit pattern (an int) and then convert that into a float. Doing this the obvious way ( (float)rand.nextInt() ) does not work because Java tries


Because the binary64 floating point numbers are not uniformly spaced, you cannot generate a uniform distribution which can return all possible values less that 1.

If you omit the requirement uniform you have to generate all representable multiples of the smallest positive denormal number 2^(-1074)and zero.

Java Number Cruncher: The Java Programmer's Guide to Numerical , For sequences, uniform selection of a random element, a function to generate a returning a random float in [0.0, 1.0); by default, this is the function random(). list is in selection order so that all sub-slices will also be valid random samples. The end-point value b may or may not be included in the range depending on  My goal is to generate a sequence of random float from a seeded random generator. The sequence should be the same on any machine / any compiler -- this rules out the use of std::uniform_real_distribution since it doesn't make that guarantee. Instead we have to create our own version of this, which we will be able to guarantee is portable / uses the same implementation on all platforms.


You could use brute-force rejection sampling by generating 16 random bytes and using it only if it's a valid float64 in [0,1). This approach should give you a normal distribution of all values in that range with performance not much worse than other strategies based on simple benchmarking.

For example (Go Playground):

import "math/rand"

func randFloat64() float64 {
  for {
    f := math.Float64frombits(rand.Uint64())
    if f >= 0 && f < 1.0 {
      return f
    }
  }
}

If performance is critical then you could build an enormous lookup table containing only the valid numbers and choose a random location in the table. The table could be generated ahead of time in a similar fashion as above, by enumerating the bitfield and storing only valid numbers.

Random Numbers Within a Specific Range, Therefore, a random number generator can produce values that only appear to be generates random integer values that range over all the possible positive and Random. next Float () returns a uniformly distributed random float value  The general contract of nextFloat is that one float value, chosen (approximately) uniformly from the range 0.0f (inclusive) to 1.0f (exclusive), is pseudorandomly generated and returned. All 2 24 possible float values of the form m x 2-24, where m is a positive integer less than 2 24, are produced with (approximately) equal probability.


Learn Java for Android Development, This example shows how to create an array of random floating-point numbers returns normalized values (between 0 and 1) that are drawn from a uniform distribution. value by the width of the new range, (b – a) and then shift every value by a. Some combinations of a and b make it theoretically possible for your results  Best approach, no crazed values, distributed with respect to the representable intervals on the floating-point number line (removed “uniform” as with respect to a continuous number line it is decidedly non-uniform): static float NextFloat(Random random) { double mantissa = (random.NextDouble() * 2.0) - 1.0; double exponent = Math.Pow(2.0, random.Next(-126, 128)); return (float)(mantissa * exponent); }


Comp-Informatic Practices-TB-11-R1, This explains why Math.random() generates a different sequence each time an uniformly distributed double precision floating-point value between 0.0 and 1.0 only 48 bits, this method will not return all possible 64-bit long integer values. Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1.


Random (Java Platform SE 7 ), random(). This function generates a random number from 0 to 1 such as This function can be used to generate pseudorandom floating point values. It takes no parameters and returns values uniformly distributed between 0 and 1 (including  int x = random (); float f = (double) x / RAND_MAX; The algorithmic aw is more serious and harder to x. Generating random oating-point values by division makes it impossible to produce the vast major-ity of oating-point values in the range. Of the 230 representable oating-point values between 0.0 and 1.0, this method can produce only about 226