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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:06:56+00:00 2026-05-16T11:06:56+00:00

I would like to draw a random number from the interval 1,49 but I

  • 0

I would like to draw a random number from the interval 1,49 but I would like to add a number as an exception ( let’s say 44 ) , I cannot use round(rand(1,49)) .So I decided to make an array of 49 numbers ( 1-49) , unset[$aray[44]] and apply array_rand

Now I want to draw a number from the interval [$left,49] , how can I do that using the same array that I used before ?The array now misses value 44.

  • 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-16T11:06:56+00:00Added an answer on May 16, 2026 at 11:06 am

    The function pick takes an array as an argument with all the numbers you have already picked. It will then pick a number between the start and the end that IS NOT in that array. It will add this number into that array and return the number.

    function pick(&$picked, $start, $end) {
    
        sort($picked);
    
        if($start > $end - count($picked)) {
            return false;
        }
    
        $pick = rand($start, $end - count($picked));
        foreach($picked as $p) {
            if($pick >= $p) {
                $pick += 1;
            } else {
                break;
            }
        }
    
        $picked[] = $pick;
    
        return $pick;
    }
    

    This function will efficiently get a random number that is not in the array AND WILL NEVER INFINITELY RECURSE!

    To use this like you want:

    $array = array(44); // you have picked 44 for example
    $num = pick($array, 1, 49); // get a random number between 1 and 49 that is not in $array  
    
    // $num will be a number between 1 and 49 that is not in $arrays
    

    How the function works


    Say you are getting a number between 1 and 10. And you have picked two numbers (e.g. 2 and 6). This will pick a number between 1 and (10 minus 2) using rand: rand(1, 8).

    It will then go through each number that has been picked and check if the number is bigger.

    For Example:

    If rand(1, 8) returns 2. 
      It looks at 2 (it is >= then 2 so it increments and becomes 3)
      It looks at 6 (it is not >= then 6 so it exits the loop)
      The result is: 3
    
    If rand(1, 8) returns 3
      It looks at 2 (it is >= then 2 so it increments and becomes 4)
      It looks at 6 (it is not >= then 6 so it exits the loop)
      The result is 4
    
    If rand(1, 8) returns 6
      It looks at 2 (it is >= then 2 so it increments and becomes 7)
      It looks at 6 (it is >= then 6 so it increments and becomes 8)
      The result is: 8
    
    If rand(1, 8) returns 8
      It looks at 2 (it is >= then 2 so it increments and becomes 9)
      It looks at 6 (it is >= then 6 so it increments and becomes 10)
      The result is: 10
    

    Therefore a random number between 1 and 10 is returned and it will not be 2 or 6.

    I implemented this a long time ago to randomly place mines in a 2-dimensional array (because I wanted random mines, but I wanted to guarantee the number of mines on the field to be a certain number)

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

Sidebar

Related Questions

I would like to use Cocos2d on the iPhone to draw a 2D car
I would like to load and draw multiple/all images from a directory in Processing.
I would like to draw a 3D segment from point (x1, y1, z1) to
I would like to draw a round pie in a rectangle figure. At the
So I would like to draw a 2D point in OpenGL, but not in
I would like to draw on a CGContext by using tiles from CGImage that
in Android I would like to draw a PieChart with a dynamically number of
I would like to draw lines (of arbitrary position and length) onto a surface
I would like to draw some shapes on a JPanel by overriding paintComponent .
I would like to draw a chart in OpenGL similar to the donut graph

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.