SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating 1000 Unique Random strings

    Hi,

    I need to use PHP to generate a number of unique and random strings which include numbers and letters and are around 8 characters.

    Has anybody had to do it before and what techniques did they use to create the unique randoms?

    I have thought of using a unique timestamp (timestamp+microseconds) and random word combo then using a hashing technique, but examples of what others have done would be handy.

  2. #2
    SitePoint Zealot devscripts's Avatar
    Join Date
    Sep 2004
    Location
    ur monitor
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $junk = md5(time());
    "None of you [truly] believes until he wishes for his brother what he wishes for himself." By The one and Only Prophet Mohammed (Peace and blessings be upon him)

  3. #3
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, I need to create 1000 unique strings though, time() in a for loop doesn't quite do it.

    How about -
    PHP Code:
    for($i 0$i 1000$i++)
    {
       
    $unique_id substr(md5(time() . microtime()), 010);

    Will that create a unique id? (Given that it is only grabbing the first 10 numbers of the md5 hash.

    Is there a better way to do this?

  4. #4
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For anyone interested - this works well

    PHP Code:
    $array = array();
    for(
    $i 0$i 10000$i++)
    {
        echo 
    mk_unique_key(3$array)."<br>";
    }

    function 
    mk_unique_key($size$array) {
        global 
    $array;
        
    $key substr(md5(microtime()),0,$size);
        
        if (
    in_array($key,$array)) {
             echo 
    "<p>DUPE!, $key already in! Recalculating</p>";
            
    $key mk_unique_key($size,&$array);
        }
        
    $array[] .= $key;
        return 
    $key;


  5. #5
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Instead of a global, you'd be better off using a static variable.

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could try this

    PHP Code:
    <?php

    $number 
    1000;
    $length 8;

    $keys generate_strings($number$length);
    // $keys is an array of random strings (1000 elements).

    function generate_strings($number$lenth) {


        
    mt_srand(10000000 * (double)microtime() * $lenth); // Generate a randome string
        
        
    $salt    "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXY3456789"// the characters you want to allow
        
        
    $len    strlen($salt);
        
        
    $strings = array();
        
        for(
    $i 0$i $number$i++) {
        
            
    $string null;
            
            for(
    $j 0$j $length$j++) { //if you want to change the length of each string, do it here. You could randomise the string length by replacing $length, with mt_rand(6, 10); This would create random string lengths from 6-10 characters in length.
            
                
    $string .= $salt[mt_rand(0$len 1)];
            
            }
        
            
    $strings[] = $string;
        
        }
        
        return 
    $strings;

    }

    ?>

  7. #7
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Bling! That looks good - and its guaranteed to be unique in 1000 samples?

  8. #8
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whops forgot about the unique part (that code has a slim chance of creating a duplicate string.

    Change
    PHP Code:
    $strings[] = $string
    To

    PHP Code:
    if(in_array($string$strings)) {

    $number++;

    } else {

    $strings[] = $string;




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
  •