Is it possible to find random floats in the range [a, b] in python?

I am trying to generate random floats in the range [0.8.0.9] in python, but, unfortunately, all found tools can generate randoms in the range [a, b) for float. (e.g. Random.uniform(a,b))

Meanwhile, I tried to do something like this:

uniform(0.8,0.90000000001)

but it is really bad

any ideas?

+3
source share
5 answers

The difference between [0.8, 0.9] and [0.8.0.9] is vanishingly small. Given the limitations of binary floating point, I don't think there is even a difference, since 0.9 cannot be represented exactly anyway. Use [0,8,0,9).

+7
source

random.uniform() docstring:

[a, b) [a, b] .

, , . , [0,8, 0,9] 900719925474100. , , , ​​ .

+3

. , Random.uniform(0.8,0.9)? ( , , "0.8" "0.9" 8/10 9/10?)

, ; , , 99,9% , , : Random.uniform(a,b) .

+2

docs , uniform(a, b) [a, b] 1 , -, , , random(), , , [0.0, 1.0) 2.

[a, b], :

random.inclusive_uniform = lambda a, b:
    random.randint(0, 0xFFFFFFFF) / float(0xFFFFFFFF) * (b - a) + a

"" , , ( 0.8, 0.9 IEEE-754), [a ', b'], a ' b' - , , b . , , [a, b], !

1 , , N a <= N <= b.

2 , , , 0.0, , (0.0, 1.0).

+1

, :

def uniform_closed(a, b):
     while True:
         r = random.uniform(a, b + (b - a) * 0.01)
         if r <= b: return r
+1

Source: https://habr.com/ru/post/1537166/


All Articles