random.randrange(start, stop)
only takes integer arguments. So how would I get a random number between two float values?
7 Answers
Use random.uniform(a, b):
>>> import random
>>> random.uniform(1.5, 1.9)
1.8733202628557872
-
9could this theoretically produce 1.5 and 1.9? or would it only ever produce 1.50~1 and 1.89~? Apr 29, 2016 at 13:39
-
34@Musixauce3000 Short Answer: Yes. Longer answer: If you look at the documentation it states
Returns a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a
In other words the outputN
can equal either inputa
andb
. In this case1.5
and1.9
.– DanMar 1, 2017 at 13:37 -
Is there another way to do this without using the
.uniform
function, but instead with either.random
orrandrange
? Mar 27, 2019 at 10:12 -
1@DerryckDX
1.5 + random.random() * (1.9 - 1.5)
should do it, even though according to the specs this will never return exactly1.9
(even in theory). Mar 29, 2019 at 0:03 -
2@Musixauce3000 it seems
uniform(a, b)
is implemented asa + (b-a) * random()
and returns a random number in the range [a, b) or [a, b] depending on rounding github.com/python/cpython/blob/…– PavelJun 4, 2019 at 8:47
if you want generate a random float with N digits to the right of point, you can make this :
round(random.uniform(1,2), N)
the second argument is the number of decimals.
random.uniform(a, b)
appears to be what your looking for. From the docs:
Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.
See here.
-
What if I want to generate an number as follow: Range of azimuth angle is 0 to 2*Pi radians excluding 2*pi viz., in mathematics range
[0, 2pi)
?– RSWAug 2, 2022 at 6:55 -
@RajeshSwarnkar
random.random() * 2*math.pi
, as the doc says the random function "Return[s] the next random floating point number in the range 0.0 <= X < 1.0" Feb 15 at 9:23
From my experience dealing with python, I can only say that the random function can help in generating random float numbers. Take the example below;
import random
# Random float number between range 15.5 to 80.5
print(random.uniform(15.5, 80.5))
# between 10 and 100
print(random.uniform(10, 100))
The random.uniform() function returns a random floating-point number between a given range in Python
The two sets of code generates random float numbers. You can try experimenting with it to give you what you want.
Most commonly, you'd use:
import random
random.uniform(a, b) # range [a, b) or [a, b] depending on floating-point rounding
Python provides other distributions if you need.
If you have numpy
imported already, you can used its equivalent:
import numpy as np
np.random.uniform(a, b) # range [a, b)
Again, if you need another distribution, numpy
provides the same distributions as python, as well as many additional ones.
Use this to get random floating point number between range n to m:
import random
random.uniform(n,m)
If you want to get a random float number up to x decimal places you can use this instead:
import random
round(random.uniform(n, m), x)
For completness sake: If you use numpy, you can also call the uniform
method on an instance of random number generator (now the preferred way in numpy when dealing with random numbers).
import numpy as np
seed = 42
low = 0.12
high = 3.45
rng = np.random.default_rng(seed)
rng.uniform(low, high)
np.random.uniform(start, stop)
ornp.random.uniform(start, stop, samples)
if you wanted multiple samples. Otherwise below answers are best.[0 , 2*pi)
?