开发者

I was wondering how can I generate random numbers and letters together using PHP

how can I generate random numbers and letters mixed together.

Here is my开发者_开发问答 php code.

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


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;
} 


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.


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


You need something this:

   $chars = 'ABCDEFGHIJKLMNOPQRSTOUVWXYZ0123456789';
    $i = 0;
    do{
    $i++;
    $ret .= $ret.$chars[mt_rand(0,35)];
    }while($i<$length+1);


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.


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;
}
?>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜