Python Forum

Full Version: random.uniform is not working correctly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 4 values:

left_latitude = 32.36825432569349
right_latitude = 32.367919037002075
up_latitude = 32.36844462412729
down_latitude = 32.367774046912494
I need to take one value that will be in a range of all these four values it's mean it can't be bigger than all these values.
So I use random.uniform:

left_right_latitude = random.uniform(right_latitude, left_latitude)
up_down_latitude = random.uniform(down_latitude, up_latitude)
print(left_right_latitude > up_down_latitude)
latitude = random.uniform(left_right_latitude, up_down_latitude)
print(latitude > left_latitude)
print(latitude > right_latitude)
print(latitude > up_latitude)
print(latitude > down_latitude)
print(latitude)
False
True
False
True
32.36817441682401
As you can see I got the value that bigger than right_latitude and down_latitude.
But if I will change the values of all latitude for example to:

left_latitude = 8.9
right_latitude = 5.5
up_latitude = 25.7
down_latitude = 12.5
False
False
False
False
False
15.13427432226921
everything is works perfect.
Where is my mistake? Huh Huh
I don't understand the concept of left/right/up/down when it comes to latitude.
assuming up is north and down is south, values are from -90 to 90. so 4 values can be ordered uniquely in ascending order (creating 5 intervals within that range), where do you want random values?
With simpler notations, you are doing
def func(a, b, c, d):
    x = uniform(a, b)
    y = uniform(c, d)
    z = uniform(x, y)
    return z
With this definition, func() can return any value between min(a, b, c, d) and max(a, b, c, d). In particular you cannot expect a special relation between z and the values in the middle of the 4 arguments.
(Feb-06-2020, 02:30 PM)buran Wrote: [ -> ]I don't understand the concept of left/right/up/down when it comes to latitude.
assuming up is north and down is south, values are from -90 to 90.

don't take latitude as latitude you can ignore, take it as a simple variable
(Feb-06-2020, 02:34 PM)dron4ik86 Wrote: [ -> ]don't take latitude as latitude you can ignore, take it as a simple variable
mmm, the question still remains. 4 values can be order uniquely, creating 5 intervals from - infinity to +infinity. up/down/left/right doesn't make sense. If you designate them (in ascending order) a,b,c,d where a<=b<=c<=d, where do you want you random number n to come from
1. n < a
2. a <= n < b
3. b <= n < c
4 c <= n < d
5 d <= n
(Feb-06-2020, 02:31 PM)Gribouillis Wrote: [ -> ]With simpler notations, you are doing
def func(a, b, c, d):
    x = uniform(a, b)
    y = uniform(c, d)
    z = uniform(x, y)
    return z
With this definition, func() can return any value between min(a, b, c, d) and max(a, b, c, d). In particular you cannot expect a special relation between z and the values in the middle of the 4 arguments.


a = 32.36825432569349
b = 32.367919037002075
c = 32.36844462412729
d = 32.367774046912494


def func(a, b, c, d):
    x = random.uniform(a, b)
    y = random.uniform(c, d)
    z = random.uniform(x, y)
    return z


print(func(a=a, b=b, c=c, d=d))
print(a > func(a=a, b=b, c=c, d=d))
print(b > func(a=a, b=b, c=c, d=d))
print(c > func(a=a, b=b, c=c, d=d))
print(d > func(a=a, b=b, c=c, d=d))
Same problem
True
False
True
False
dron4ik86 Wrote:Same problem
It is not a problem. You cannot expect that the value returned by this algorithm satisfies the inequalities that you wrote. For example if you call uniform(1, 5), it is not a problem if the result is > 2 or < 2
(Feb-06-2020, 02:43 PM)Gribouillis Wrote: [ -> ]
dron4ik86 Wrote:Same problem
It is not a problem. You cannot expect that the value returned by this algorithm satisfies the inequalities that you wrote. For example if you call uniform(1, 5), it is not a problem if the result is > 2 or < 2

So how can I solve this problem? I need to get any value that will be not bigger than any value in variables?
So you need a value that will be smaller than min(a, b, c, d)? Then choose a random number below this value, for example random.uniform(-10000000, min(a, b, c, d))
(Feb-06-2020, 02:54 PM)Gribouillis Wrote: [ -> ]So you need a value that will be smaller than min(a, b, c, d)? Then choose a random number below this value, for example random.uniform(-10000000, min(a, b, c, d))

thanks