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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:01:43+00:00 2026-06-09T00:01:43+00:00

I’ve been asked to generate all possible combinations in the range AA0000-ZZ9999(let’s call it

  • 0

I’ve been asked to generate all possible combinations in the range AA0000-ZZ9999(let’s call it folio), for each combination I also need a 8 digits unique ramdom number (let’s call it attcode), can’t be consecutive, I know that there are a lot of combinations and the process will be slow, but as I’m using the rand function, and I’ve to validate that every attcode has to be unique, it is making my code slower, so if is it possible (I know it is, just don’t know how), give me recommendations ’bout how can I improve this in my code

$alph = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1);
$code_cache = array();
foreach ($alph as $value1) {
  foreach ($alph as $value2) {
    for ($i = 0; $i <= 9; $i++) {
        $n=$i;
        if($i<10){
            $n="000".$i;
        }
        elseif($i<100){
            $n="00".$i;
        }
        elseif($i<1000){
            $n="0".$i;
        }
        $code = rand(10000000, 99999999);
        while(in_array($code, $code_cache)){
            $code = rand(10000000, 99999999);
        }
        $code_cache[]=$code;
        echo $value1.$value2.$n.'-'.$code.'<br/>';
    }
  }
}
  • 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-09T00:01:48+00:00Added an answer on June 9, 2026 at 12:01 am

    OK, I’ve totally cracked it this time. I’m actually a bit pleased with myself on this one:

    // An array of 10000 0's
    $attcodes1 = array_fill(0, 9999, 0);
    
    // An array of 10000 from 0 - 9999
    $attcodes2 = range(0, 9999);
    // Not actually necessary but makes $attcodes appear more random
    shuffle($attcodes2);
    
    // Loop until the alphas roll over to 3 characters
    for ($alpha = "AA", $num = 0; $alpha != 'AAA'; $num++) {
      if ($num == 1001) {
        $num = 0; // At 1000 reset the counter to 0
        $alpha++; // Roll over to next alpha sequence
      }
      $folio = sprintf("$alpha%04s", $num); // Generate folio
    
      // Here's the clever bit, if I do say so myself...
      // Loop while we are hitting 4 digit sequences that have used every other
      // possible 4 digit sequence and remove them from the options.
      // This is *very* unlikely to loop more than twice, if ever
      while ($attcodes1[$part1 = array_rand($attcodes1)] >= 9999) {
        array_splice($attcodes1, $part1, 1);
      }
      // Get a 4 digit sequence not used with $part1 before and make sure we never
      // get it again (increment counter)
      $part2 = $attcodes2[$attcodes1[$part1]++];
      // Now it just needs stitching together and left-padding with 0s
      $attcode = sprintf("%04s%04s", $part1, $part2);
    
      // Job done
      echo $folio.'-'.$attcode."\n";
    
    }
    

    …and all without the insane memory usage my early attempts were generating. Seriously, it takes 24 bytes to store a 32 bit (4 byte!) integer in PHP.

    Looking at the progress on my (pretty low spec) laptop, I’m estimating a 10 minute runtime from start to finish. This will decrease considerably if you don’t echo the results on the fly. I think. Although I’m not sure what you’d do with them instead without burning up memory or getting stuck on disk I/O.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.