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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:48:31+00:00 2026-06-03T00:48:31+00:00

I am implementing a system at the moment where it needs to allocate a

  • 0

I am implementing a system at the moment where it needs to allocate a number in a certain range to a person, but not use any number that has been used before.
Keep in mind, both the number range and exclusion list are both going to be quite large.

Initially, I thought doing something like this would be best:

<?php 
  $start = 1;
  $end = 199999;
  $excluded = array(4,6,7,8,9,34);
  $found = FALSE;
  while (!$found) {
    $rand = mt_rand($start,$end);
    if (!in_array($rand,$excluded)) {
      $found = TRUE;
    }
  } 
?>

But I don’t think this is ideal, there is the possibility of an infinite loop (or it taking a very long time / timing out the script).

I also thought about generating an array of all the numbers I needed, but surely a massive array would be worse? Also doing an array diff on 2 massive arrays would surely take a long time too?
Something like this:

<?php 
  $start = 1;
  $end = 199999;
  $allnums = range($start,$end);
  $excluded = array(4,6,7,8,9,34);
  $searcharray = array_diff($allnums,$excluded);
  $rand = array_rand($searcharray);
?>

So, my question would be which would be a better option? And is there another (better) way of doing this that someone has used before?

  • 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-03T00:48:32+00:00Added an answer on June 3, 2026 at 12:48 am

    Array’s holding large amounts of data will use up a lot of memory, can you not use a database to hold these numbers in? That’s generally what they are designed for.

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

Sidebar

Related Questions

I am implementing a breakpoint system for use in my Python development that will
I am implementing a system for touchscreens in java that needs to be able
I've been thinking of implementing this system, but can't help but feel there's a
We are implementing a system that analyses books. The system is written in PHP,
I am implementing a betting system and each user has a balance , how
I am implementing a tagging system for a website that uses JDO . I
I'm implementing a Announcement system that is loosely based on this article: https://web.archive.org/web/20211020111733/https://www.4guysfromrolla.com/articles/110409-1.aspx One
I'm implementing a slug system for my website at the moment. I plan to
I'm implementing a vote system and I want to use jQuery. I have the
We are implementing a reporting system (.net 4.0, c#) that contains all the sender/recipient/timestamp

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.