Python Forum
random.uniform is not working correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.uniform is not working correctly
#1
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
Reply
#2
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
(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
Reply
#5
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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
Reply
#7
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
Reply
#8
(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?
Reply
#9
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))
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If statement not working correctly? MrKnd94 2 800 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  Finding MINIMUM number in a random list is not working Mona 5 3,008 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  random selection of song and artist not working Unknown_Relic 2 2,318 Nov-08-2019, 02:06 PM
Last Post: perfringo
  Working with Random Generated Numbers YoungGrassHopper 4 3,138 Sep-10-2019, 06:53 AM
Last Post: YoungGrassHopper
  How do you know when a library is working correctly? grjmmr 1 2,387 Jul-24-2018, 03:08 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020