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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:34:26+00:00 2026-06-02T10:34:26+00:00

I need to get a random number that is between 0 – 80 and

  • 0

I need to get a random number that is between

0 - 80

and

120 - 200

I can do

$n1 = rand(0, 80);
$n2 = rand(120, 200);

But then I need to choose between n1 and n2. Cannot do

 $n3 = rand($n1, $n2)

as this may give me a number between 80 - 120 which I need to avoid.

How to solve this?

  • 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-02T10:34:28+00:00Added an answer on June 2, 2026 at 10:34 am

    Since both ranges have different sizes (even if only by 1 number), to ensure good random spread, you need to do this:

    $random = rand( 0, 200 - 39 );
    if ($random>=120-39) $random+=39;
    

    Fastest method. 🙂

    The way this works is by pretending it’s a single range, and if it ends up picking a number above the first range, we increase it to fit within the second range. This ensures perfect spread.

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

Sidebar

Related Questions

I need to create a function that will generate 2 random numbers between x
I heard that computation results can be very sensitive to choice of random number
I'm in need of a C++ (pseudo, i don't care) random number generator that
I need to get some random colors to draw a pie. My code works
I have code that generates a random number form 0-1 3 times and I
I need to create a random number of dropdownlists in my view, based on
I need to generate about 5,000 random numbers in my script, but the CPU
Let say that I would like to get an random double value wich is:
I need to randomly generate a number of values in a batch file that
for my selenium tests I need an value provider to get a 5-digit number

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.