Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:09:10+00:00 2026-06-13T00:09:10+00:00

I am trying to create random Lat/Lngs inside a bounding box. The below code

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:09:11+00:00Added an answer on June 13, 2026 at 12:09 am

    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:

    rng = Random.new(seed)
    lat = minLat + rng.rand * (maxLat - minLat)
    lng = minLng + rng.rand * (maxLng - minLng) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a random generator and on another page if found a
I am trying to create random numbers from a lognormal distribution using numpy/scipy. The
I'm trying to create a random float between 0.15 and 0.3 in Objective-C. The
So, I'm trying to create a random vector (think geometry, not an expandable array),
I'm trying to figure out a way to create random numbers that feel random
I'm trying to create a sensible class hierarchy for a congruential random number generator
Hey im trying to create a random way to pick a team of 4
Trying to create a table for quantiles of the sum of two dependent random
I am trying to create a transparent image canvas and then place other random
I'm trying to create a simple loop that adds a random number of stars,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.