Why does random.uniform(0, float("inf")) always return inf?

numpy random uniform
random uniform distribution
numpy random uniform integer
numpy random normal
np.random.uniform seed
numpy random number between 0 and 1
python random float
numpy random array of floats

I was playing around with the random module in python and I found that this code always returns inf:

import random

print(random.uniform(0, float("inf")))

I was wondering why and if there is another way of doing this so that it does not always return inf.

If you investigate random.uniform source it will be clear:

def uniform(self, a, b):
    "Get a random number in the range [a, b) or [a, b] depending on rounding."
    return a + (b-a) * self.random()

Any arithmetic with float('inf') returns -inf or inf.

In this case:

b-a where b is inf evaluates to inf, inf * self.random() is inf (or -inf if self.random() is negative) and a + inf is inf.

In python, what is the difference between random.uniform() and , random. uniform(0, 1) is basically the same thing as random. random() (as 1.0 times float value closest to 1.0 still will give you float value closest to 1.0 there is no possibility of a rounding error there). Whereas random. numpy.random.uniform¶ numpy.random.uniform (low=0.0, high=1.0, size=None) ¶ Draw samples from a uniform distribution. Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.

I'm not really sure why you would want a random number in an unspecified range, but maybe for practical purposes using something like sys.maxsize as the upper bound could be good enough.

numpy.random.uniform — NumPy v1.15 Manual, The default value is 0. high : float or array_like of floats. Upper boundary of the output interval. All values generated will be less than high� numpy.random.uniform¶ numpy.random.uniform(low=0.0, high=1.0, size=None)¶ Draw samples from a uniform distribution. Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.

Picking a value uniformly at random in an infinite range is equivalent to picking an element uniformly at random from an infinite set. Uniform means that all your elements have the same probability p of being selected. If p is positive, then the sum of the probabilities in your distribution is equal to p*inf, which is infinite, and therefore not equal to 1.

As it as been mentioned in the accepted answer, reading the code explains why this particular implementation returns inf but this is not a bug, that's the only logical thing this function call could return. But in my opinion this should just raise an error.

Note that you may interpret random float generation (in any interval) to be equivalent to picking an element from an infinite set, but that's not the case because we are not using infinite precision float numbers (they are not real numbers). This is what makes a call to random.uniform(0, 1) well defined.

Python Get Random Float Numbers using random and Uniform , In this example, we will see how to generate a random float number between 0 to 1 using random.random() function. This function generates a� Random float array 2X3 [[0.34344184 0.8833125 0.76322675] [0.17844326 0.7717775 0.86237081]] Create an n-dimensional array of float numbers between any float range. Here we will use Use a numpy.random.uniform() function to generate a random 2×2 array.

numpy.random.uniform — NumPy v1.9 Manual, low : float, optional. Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0. high : float. Python number method uniform() returns a random float r, such that x is less than or equal to r and r is less than y. Syntax. Following is the syntax for uniform() method − uniform(x, y) Note − This function is not accessible directly, so we need to import uniform module and then we need to call this function using random static object

random — Generate pseudo-random numbers — Python 3.8.5 , The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random() . To shuffle an immutable� Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval. To sample Unif[a, b), b > a multiply the output of random_sample by (b-a) and add a:

9.6. random — Generate pseudo-random numbers — Python 2.7.18 , For sequences, uniform selection of a random element, a function to It produces 53-bit precision floats and has a period of 2**19937-1. The optional argument random is a 0-argument function returning a random float in� The following are 40 code examples for showing how to use random.uniform().These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.