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

  • Home
  • SEARCH
  • 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 754765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:03:06+00:00 2026-05-14T15:03:06+00:00

My code is to generate random cordinates of lat and long within a bound:

  • 0

My code is to generate random cordinates of lat and long within a bound:

        Random lastLat = new Random();

        Random lastLon = new Random();

        for (int i = 0; i < 50; i++)
        {
            int lat = lastLat.Next(516400146, 630304598); //18.51640014679267 - 18.630304598192915

            int lon = lastLon.Next(224464416, 341194152); //-72.34119415283203 - -72.2244644165039

            SamplePostData d0 = new SamplePostData();
            d0.Location = new Location(Convert.ToDouble("18." + lat), Convert.ToDouble("-72." + lon));
            AddPushpin(d0);

        }

My output looks like this:
http://img263.imageshack.us/img263/7504/capturerkv.png http://img263.imageshack.us/img263/7504/capturerkv.png

Is there something wrong with how my numbers are generated?

  • 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-05-14T15:03:07+00:00Added an answer on May 14, 2026 at 3:03 pm

    Jørn’s answer gave the problem, but not the solution: just use a single instance of Random:

    Random rng = new Random();
    
    for (int i = 0; i < 50; i++)
    {
        int lat = rng.Next(516400146, 630304598);
        int lon = rng.Next(224464416, 341194152);
        SamplePostData d0 = new SamplePostData();
        d0.Location = new Location(18d + lat / 1000000000d,
                                   -72d - lon / 1000000000d);
        AddPushpin(d0);
    }
    

    (I’ve changed the way of using the result, too – there’s no need to convert to and from a string to achieve what you want. An alternative would be to call Random.NextDouble instead and multiply it by the size of the desired range, then add a base value.)

    If you’re calling routine multiple times, you should probably use a single instance of Random across those multiple calls. Beware though: Random in .NET is not thread-safe. Ideally you should have a single instance per thread. See my article on randomness for more details of how to handle Random.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to generate random numbers in range... int randomNumberWithinRange(int min,int
I'm looking for some succinct, modern C# code to generate a random date between
I wrote some Java code to generate pseudo-random numbers but I'm running into what
I've written some code to generate a sequence of random characters, but it does
For an ecommerce site I want to generate a random coupon code that looks
I've been using this code to generate a random number with js: var max
I need to generate random UNIQUE numbers within a range, how can I do
The following code will generate a link to the page I want to get
I'm trying to generate code coverage reports with EMMA using tests of which some
I have some very simple code to generate an assembly and invoke a method

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.