Thousands of Random Unique Numbers

Discussion in 'PHP' started by tegan, Nov 7, 2008.

  1. #1
    I'm searching for an efficient way to create thousands of unique random numbers. I would like them to be either all numeric, or a mix of numbers and letters. I would also like to keep the character length around 6 to 10 characters.

    Does anybody have a good idea?
     
    tegan, Nov 7, 2008 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    rand() function would help you
     
    crivion, Nov 7, 2008 IP
  3. tegan

    tegan Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I may have figured out the answer to my own question.

    http://www.codewalkers.com/c/a/Miscellaneous-Code/Multiple-Unique-Random-Numbers/
     
    tegan, Nov 7, 2008 IP
  4. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    function CreateRandomString($instances,$min_length,$max_length)
        {
            $chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQUSTUVWXYZ";
            # srand((double)microtime()*1000000); # seed not needed since => php 4.2.0
            $strings = Array();
            for ($inst=0;$inst<=($instances-1);$inst++)
                {
                    $i = 0;
                    $pass = '' ;
                    $randlength = rand($min_length,$max_length);
                    while ($i <= $randlength)
                        {
                            $num = rand() % 33;
                            $tmp = substr($chars, $num, 1);
                            $pass = $pass . $tmp;
                            $i++;
                        }
                    $strings[$inst] = $pass;
                }
            return $strings;
        }
    #
    # test it out
    #
    print_r(CreateRandomString(1000,6,10));
    #
    # How to use it:
    #   $instances:  nubmer of random strings you want to create.
    #   $min_length: minimum string length.
    #   $max_length: maximum string length.
    #   $chars: the charactor database to pick from at random
    ?>
    
    PHP:
     
    Bind, Nov 7, 2008 IP
  5. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    If you are storing them in variables, be sure that you don't run out of memory.
     
    rohan_shenoy, Nov 7, 2008 IP