stick
stick

Reputation: 45

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

Upvotes: 0

Views: 1168

Answers (7)

berkes
berkes

Reputation: 27553

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.

Upvotes: 2

Michael Parkin
Michael Parkin

Reputation: 51

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

Upvotes: 2

Pukhraj Prajapat
Pukhraj Prajapat

Reputation: 95

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

Upvotes: 0

kander
kander

Reputation: 4286

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

Upvotes: 0

Rimian
Rimian

Reputation: 38418

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.

Upvotes: 0

turbod
turbod

Reputation: 1988

You need something this:

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

Upvotes: 0

ahmetunal
ahmetunal

Reputation: 3950

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

Upvotes: 5

Related Questions