I am trying to create random Lat/Lngs inside a bounding box. The below code creates Lat/Lngs and they plot on the map… and while they are in the right country, they are not in the right “box”
my bounding box looks like this:
top_left = 51.514881121099904, -0.1293877362387679
bottom_right = 51.492151803709525, -0.09693947143827275
minLat = 51.514881121099904
maxLat = 51.492151803709525
minLng = -0.09693947143827275
maxLng = -0.1293877362387679
My code looks like this:
markers = []
10.times do
x = minLat + rand(maxLat - minLat)
y = minLng + rand(maxLng - minLng)
latLng = [x,y]
markers << latLng
end
markers.each do |x|
puts "#{x[0]} , #{x[1]}"
end
But its not outputting the right lat/Lngs. Is there something I am doing wrong? Am I looking at this the wrong way?
For example one run outputs:
51.51778835694538 , 0.8479098023328967
51.56834519895925 , 0.70003581382595
52.47933844269927 , 0.6167209565236026
51.52116824209034 , 0.8997051592766233
52.26830135905681 , 0.7514635122980192
52.143607260212896 , 0.21838042686322268
52.04649651836466 , 0.6683244476203801
52.057410769358576 , -0.08734906181439461
52.053408170846915 , 0.1600069328435182
51.8991829618331 , 0.3993135670912452
But clearly 52.26830135905681 is not between the min and max lat… so I am a little confused
Does rand() do some sort or rounding up. Can it work on such large floats?
How is rand() defined? Can’t remember off the top of my head…
You can use ruby’s Random class (http://www.ruby-doc.org/core-1.9.3/Random.html) and do something like this: