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 7632951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:45:24+00:00 2026-05-31T06:45:24+00:00

I need to generate a random pairs of numbers (floats) , within a certain

  • 0

I need to generate a random pairs of numbers (floats) , within a certain range .
Basically those are points for [Lat,Lng] pairs (Longitude – latitude coordinates)

I thought This would be very straight forward with

<?php echo (rand(0,60).'.'.rand(0,99999999999999).' ||  '); // yes, a string is ok...?>

but it did not give me the control over how many numbers after the float point (resolution) – which I need fixed.

So the next phase was :

<?php echo (rand(0,60*pow(10,6)))/pow(10,6).' ||  '; //pow(10,$f) where $ is the resolution ?>

and this works . sort of ..
sometimes it produces results like

22.212346 ||  33.134 ||  36.870757 ||  //(rare , but does happen)

but hey – coordinates are from -90 to 90 (lon) and -180 to 180 (lan) – what about the minus ?

echo (rand(0,-180*pow(10,9)))/pow(10,9).' ||  ';

that should do it .. and combining all together should give me somehow a random string like

23.0239423525 || -135.937419777

so after all this introduction – here is Are my question(s) .

  • Being the newbie that I am – am I missing something ? is there no built-in function to generate random floats with a negative to positive range in PHP ?

  • Why is the function above sometimes turns only resolution 3,4 or 5 if it is told to return 6 (i did not apply any ABS or ROUND) – is there an automatic rounding in php ? and if there is , how to avoid it ?

  • I have noticed that the “random” is not so random – the generated numbers are always more or less series between a range – close to one another . is the PHP random a simple very-very-very fast rotating counter ?

  • how do I EXCLUDE a range from this generated range ?? (or actually and array of ranges)

I know these are a lot of questions, but any help / thought would be great ! (and if the first one is answered positively, the rest can almost be ignored 🙂

  • 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-31T06:45:26+00:00Added an answer on May 31, 2026 at 6:45 am

    The rand() function can take a negative min so maybe you can do this:

       $num = mt_rand(-180000000, 180000000)/1000000;
       echo number_format($num, 6);
    

    if you want 6 places after decimal point.

    In terms of excluding a range you may have to do it in two steps. First consider the ranges that you do want. Lets say you have 3 ranges from which you want to generate the random number. range 1 = -180 to -10, range 2 = 10 – 100 and range 3 = 120 – 180. Then you can generate a random number from 1 to 3 inclusive, use that to pick one range and then generate the number in that range.

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

Sidebar

Related Questions

I need to generate random UNIQUE numbers within a range, how can I do
Like my question, i need to generate random numbers that have identical pairs between
I need to generate a lot of random numbers. I've tried using random.random but
In a Python program, I need to generate normally-distributed random numbers with a specific,
Here is a fun one: I need to generate random x/y pairs that are
I need to generate random numbers from Binomial(n, p) distribution. A Binomial(n, p) random
I need to use random class to generate random numbers in a multi threaded
I need to generate random numbers based on multiple seed values, need some ideas
I need to generate some random numbers in C for testing and debugging the
I need to generate random BigDecimal value from given range. How to do it

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.