HOWTO: Create a random alpha-numeric string in PHP

Creating an alpha-numeric string in PHP is easy, but having a quick function you can call is better!

This is very useful for things like password generators.

To see how to do this in Python, check out this post: …howto-create-a-random-alpha-numeric-string-in-python

To see how to do this in Perl check out this post: …howto-create-a-random-alpha-numeric-string-in-perl


<?php
function makeID($size = 6) {
    global $makeIDpool;
    $makeIDpool = array_merge(range(0,9), range('a','z'), range('A','Z'));
    $append = function ($value){ global $makeIDpool; return $makeIDpool[rand(0,count($makeIDpool) - 1)]; };
    return implode('', array_map($append, range(0,$size - 1)));
}
?>

Example:

This code sample:


print makeID().'<br />';
print makeID(10).'<br />';
print makeID(20).'<br />';
print makeID(1).'<br />';

Will print something like this:

KNHtkn
2z8ksLDSkp
bfn5X5jF5dTZ3itt9HWy
q

Did you find this post useful or have questions or comments? Please let me know!

Posted in How Tos, php, Programming | Leave a comment

HOWTO: Create a random alpha-numeric string in Perl

Creating an alpha-numeric string in Perl is easy, but having a quick function you can call is better!

This is very useful for things like password generators.

To see how to do this in Python, check out this post: …howto-create-a-random-alpha-numeric-string-in-python

To see how to do this in PHP, check out this post: …howto-create-a-random-alpha-numeric-string-in-php


sub makeID {
    my $makeIDchars = ( $#_ >= 0 ? shift @_ : 6);
    my @makeIDpool = ('a'..'z','A'..'Z',0..9);
    return join '', map $makeIDpool[int(rand($#makeIDpool - 1))], 1..$makeIDchars;
}

Example:

This code sample:

print makeID()."\n";
print makeID(10)."\n";
print makeID(20)."\n";
print makeID(1)."\n";

Will print something like this:

QLNbMX
1wO6uBRbER
bjbYBJSMLONKOPU0SiQS
p

Did you find this post useful or have questions or comments? Please let me know!

Posted in How Tos, perl, Programming | Leave a comment

HOWTO: Create a random alpha-numeric string in Python

Creating an alpha-numeric string in python is easy, but having a quick function you can call or import as a module is better!

This is very useful for things like password generators.

To see how to do this in Perl, check out this post: …howto-create-a-random-alpha-numeric-string-in-perl

To see how to do this in PHP, check out this post: …howto-create-a-random-alpha-numeric-string-in-php


def makeID(makeIDchars=6):
    import random, string
    def join(a,b): return a+b[int(random.randrange(len(b)))]
    makeIDpool = string.letters[0:52] + string.digits
    x=''
    return ''.join(map((lambda y: join(x, makeIDpool)), range(makeIDchars)))

To call it, just set a variable equal to the return value. You can optionally pass a length to get a longer or shorter string.

Example:

This code sample:

print makeID()
print makeID(10)
print makeID(20)
print makeID(1)

Will print something like this:
oaCCyB
RodUs56H7Z
bGdwsBkVMt27NvGAZTwM
B

Did you find this post useful or have questions or comments? Please let me know!

Posted in How Tos, Programming, python | Leave a comment