tags:

views:

98

answers:

7

how can I generate random numbers and letters mixed together.

Here is my php code.

$i=1;
while($i<=10000){
 echo  $i++;
}
+5  A: 

Here is the function I use

function rand_str($n = 32, $str = "abcdefghijklmnopqrstuvwxyz0123456789")
{
    $len = strlen($str);

    $pin = "";
    for($i = 0; $i < $n; $i++)
    {
        $rand    = rand(0, $len - 1);
        $letter    = substr($str, $rand, 1);
        $pin   .= $letter;
    }
    return $pin;
} 
marvin
i can't get this to work with my code :(
stick
why not simply write `$pin .= $str[$rand]`?
knittl
Better use `mb_substr` to allow multi-byte characters as well.
Gumbo
@stick: use as $myString = rand_str(12, "abcdef");First parameter is how many random characters you want, and the second one is which characters should be included.@Gumbo: you are right, that would be much better, thanks
marvin
@stick How much experience with PHP do you have?
ceejayoz
A: 

You need something this:

   $chars = 'ABCDEFGHIJKLMNOPQRSTOUVWXYZ0123456789';
    $i = 0;
    do{
    $i++;
    $ret .= $ret.$chars[mt_rand(0,35)];
    }while($i<$length+1);
turbod
What if *length* is set to 0?
Gumbo
return 1 character.
turbod
A: 

YOu can print a random alpha numeric character like this:

print chr(rand(97, 122));

Check the ascii chars you want to return. 97 = a and 122 = z. (I think that's right)

Edit: That's almost right. You'll have to include 0-9 but that'e enough to get you started.

Rimian
A: 

Here's mine.

<?php
function randomMixed($length) {
    $output = '';
    $rand = array_merge(range('a','z'), range('A','Z'), range('0','9'));

    for($i = 0; $i < $length; $i++) {
            $output .= $rand[array_rand($rand)];
    }
    return $output;
}
?>
kander
+2  A: 

PHP offers the function uniqid(). This function guarantees a unique string. As such, the values from uniqid() are fairly predictable, and should not be used in encryption (PHPs rand(), by the way, is considered fairly unpredictable).

Running uniqid(), prefixed with rand() trough md5() give more unpredictable values:

$quite_random_token = md5(uniqid(rand(1,6)));

The other benefit of this, is that md5() assures hashes (strings) that are 32 characters/numbers long.

berkes
A: 

As told by greg0ire, you can use uniqueid() function in following way to generate alphanumeric random number: printf("uniqid(): %s\r\n", uniqid());

Pukhraj Prajapat
+2  A: 

It's normally good to have some type of string / text class that allows you to do this in a reusable fashion, rather than just writing one off functions / writing the code inline.

<?php

class Text
{
    /**
     * Generate a random string
     * @param   string   $type     A type of pool, or a string of characters to use as the pool
     * @param   integer  $length   Length of string to return
     * @return  string
     */
    public static function random($type = 'alnum', $length = 8)
    {
        $pools = array(
            'alnum'    => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'alpha'    => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'hexdec'   => '0123456789abcdef',
            'numeric'  => '0123456789',
            'nozero'   => '123456789',
            'distinct' => '2345679ACDEFHJKLMNPRSTUVWXYZ'
        );

        // Use type as a pool if it isn't preconfigured
        $pool = isset($pools[$type]) ? $pools[$type] : $type;

        $pool = str_split($pool, 1);

        $max  = count($pool) - 1;

        $str = '';

        for ($i = 0; $i < $length; $i++)
        {
            $str .= $pool[mt_rand(0, $max)];
        }

        return $str;

    }

}

here is an example usage: http://codepad.org/xiu7rYQe

Michael Parkin
to make this better you should add multibyte support and use substrings rather than array index's to find characters.. but it gives you a good start
Michael Parkin