Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: number generator

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default number generator

    I couldn't think of a good title, but I was hoping to get some help with a script that will calculate and display all possible number combinations of a 3 (or 4) digit number where the number of digits is 3, they are non repeating, and is limited to the numbers 1 - 5.

    I imagine this would not be too big of a script, but how to do this is not coming to mind.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Do you just want one at random? This will work for that:

    PHP Code:
    function randnumstr() {
        for(
    $i=1;$i<5;$i++) {
            
    $a[] = $i;
        }
        
    shuffle($a);
        return 
    implode('',$a);

    Of course you can chop that at 3 characters long if you want.


    But if you want to actually systematically display all of them, then you should go through a loop.

    There are two ways to do this: generate all possible numbers and eliminate those that have repeated digits; or generate only the valid ones. The first is easier to program, the second might be a little more efficient.

    See if that gets you started. If not, I could probably write it for you.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Thank you for that. How would it look if you wanted to gather all of the combinations and listed them sequentially. A 3 digit combination is one idea, but what about 4 or 5? Most importantly, how would it look if it were not in a function? I mention that because I am terrible with functions in php. In javascript they are easier for me to understand for some reason.

    Your code is giving me a good starting point and it is helping me to see how it would look.

    There is no real application for this script. It's just a fun exercise.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The function above is irrelevant. You could just take that wrapper off and use echo instead of return. Functions are just convenient.

    For the other, you'd need to do a loop. It would get even more complicated if you wanted everything from 1-5 digits (rather than only 5 digits). You're going to end up with something like 4 embedded loops.

    Alternatively you could use a recursive function. That'll be faster.

    Here's a probably working example (untested):

    PHP Code:
    $numbers = array();

    function 
    numberstuff($length,$prefix) { //this is ALL you need if you want every possible combination
         
    if (!is_numeric($length)||$length<1||round($length)!=$length) { return false; }
         for(
    $i=0;$i<10;$i++) {
              if (
    $length==1) {
                   
    numberstuff($length-1,$prefix.$i);
              }
              else {
                   
    $GLOBALS['numbers'][] = $prefix.$i "\n";
              }
         }
    }

    function 
    numberstuff2($length) { //this function strips out any numbers with duplicated numbers in them-- note that the max length now will be 9, because 10+ will contain duplicates and be removed
         
    numberstuff($length);
         
    $numbers $GLOBALS['numbers'];
         
    $GLOBALS['numbers'] = array(); //reset external storage array for later if needed
         
    foreach($numbers as $nkey=>$n) {
              for(
    $i=0;$i<10;$i++) {
                   if (
    substr_count($n,$i)>1) {
                        unset(
    $numbers[$nkey]);
                   }
              }
         }
         
    sort($numbers); //clean up indices, etc.
         
    return $numbers;
    }

    $mynumbers numberstuff2(5);
    print_r($mynumbers); 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    cool, thanks

    I'll give it a whirl.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    After looking at your script some more I wrote this:

    PHP Code:
    <?php
    $a
    =122;
    $b=array();
    while (
    $a<543){$a++;
    $a1=substr($a,0,1);
    $a2=substr($a,1,1);
    $a3=substr($a,2,1);
    if (
    $a3==6){$a2++;$a3=1;}
    if (
    $a2==6){$a1++;$a2=1;}
    $a="$a1"."$a2"."$a3";
    if (
    $a>543)break;
    if (
    $a1!=$a2 and $a1!=$a3 and $a2!=$a3)$b[]=$a;
    }
    echo
    "<pre>";
    print_r($b);
    echo
    "</pre>";
    ?>
    I know it doesn't lend itself too well to versatility though.
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Haha. That's easier to deal with.

    By chance, I'm working on a project tonight that needs a code like mine above. I need to find all possible combinatorial possibilities. It's very confusing. I'll try to adapt what I wrote above to make it work (and as I do so, I'll debug what I wrote, if there are problems).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Well, that's hilarious. I used == instead of >. One typo. Now it works perfectly. Confusing to debug though... it was just giving 0-9
    (I updated a bit more too, mostly the output format. Now it returns a plain array of all the numbers.)
    PHP Code:
    <?php

    $numbers 
    = array();

    function 
    numberstuff($length,$prefix='') { //this is ALL you need if you want every possible combination
         
    if (!is_numeric($length)||$length<1||round($length)!=$length) { return false; }
         for(
    $i=0;$i<10;$i++) {
              if (
    $length>1) {
                   
    numberstuff($length-1,($prefix.$i));
              }
              else {
                   
    $GLOBALS['numbers'][] = ($prefix.$i);
              }
         }
    }

    function 
    numberstuff2($length) { //this function strips out any numbers with duplicated numbers in them-- note that the max length now will be 9, because 10+ will contain duplicates and be removed
         
    numberstuff($length);
         
    $numbers $GLOBALS['numbers'];
         
    $GLOBALS['numbers'] = array(); //reset external storage array for later if needed
         
    foreach($numbers as $nkey=>$n) {
              for(
    $i=0;$i<10;$i++) {
                   if (
    substr_count($n,$i)>1) {
                   echo 
    '1';
                        unset(
    $numbers[$nkey]);
                   }
              }
         }
         
    sort($numbers); //clean up indices, etc.
         
    return $numbers;
    }

    $mynumbers numberstuff2(4);
    print_r($mynumbers);  

    ?>
    That works.
    Last edited by djr33; 04-26-2012 at 06:51 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Yep, it is working for me now too. It was not working for me earlier, but I figured that was due to my lack of understanding of how to use php functions .

    I noticed you left in a placeholder output in the first line of your output with "Array" listed at the end. Fun to try out by the way.
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oops. Debug info now edited out.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •