Avatar

:)

1
2
3
4
5
6
7
8
9
10
11
12
<?php

function random_password($length = 6)
{
	$characters = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
	shuffle($characters);
	return implode('', array_slice($characters, 0, $length));
}

echo random_password();

?>

Refactorings

No refactoring yet !

E00300dcac141ff16bee8e33401aa3af

jmut

October 17, 2007, October 17, 2007 15:45, permalink

No rating. Login to rate!

Seems this is limited to 62 char long password. I will just add more randomness in it :)

1
2
3
4
5
6
7
8
9
function random_password($length = 6)
{
	$characters = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
        srand((float)microtime() * 1000000);
	shuffle($characters);
	return implode('', array_slice($characters, 0, $length));
}

echo random_password();
28e65a85a625f7c0689bcf96ccf6043d

richardhealy

October 17, 2007, October 17, 2007 16:15, permalink

No rating. Login to rate!

Not sure what the speed differences are between using Arrays vs While statements but here's something a little different. This only uses lowercase letters though.

1
2
3
4
5
6
7
8
9
[php]
<?php
function random_password($length = 6)
{
	while (strlen($key) < $length) $key .= rand(0,1) ? chr(rand(48, 57)) : chr(rand(97, 122));
	return $key;
}
echo random_password();
?>
Avatar

Meir

October 17, 2007, October 17, 2007 16:46, permalink

2 ratings. Login to rate!

My new code is faster, a simple benchmark test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Meir:

<?php

function random_password($length = 6)
{
	$characters = implode('', array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
	$characters = str_repeat(str_shuffle($characters), $length > 62 ? ceil($length/62) : 1);
	return substr($characters, 0, $length);
}

$start = array_sum(explode(' ', microtime()));

for ($i = 0; $i < 10000; $i++) {
	random_password(100);
}

$end   = array_sum(explode(' ', microtime()));
$final = $end - $start;

echo number_format($final, 4, '.', '').' seconds'; // 0.5664 seconds

?>

richardhealy:

<?php

function random_password($length = 6)
{
	while (strlen($key) < $length) $key .= rand(0,1) ? chr(rand(48, 57)) : chr(rand(97, 122));
	return $key;
}

$start = array_sum(explode(' ', microtime()));

for ($i = 0; $i < 10000; $i++) {
	random_password(100);
}

$end   = array_sum(explode(' ', microtime()));
$final = $end - $start;

echo number_format($final, 4, '.', '').' seconds'; // 3.3504 seconds

?>
28e65a85a625f7c0689bcf96ccf6043d

richardhealy

October 17, 2007, October 17, 2007 18:18, permalink

No rating. Login to rate!

I suppose that answers that question then! lol! Interesting to know, I'm going to change my random code generator in my apps now on the back of this. Cheers Meir! :)

Avatar

Random

October 18, 2007, October 18, 2007 15:02, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
function randomString($length = 8, $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
{
    $string = '';
    $max    = strlen($characters) - 1;
    for($i = 0; $i < $length; ++$i) {
        $string .= $characters[mt_rand(0, $max)];
    }
    return $string;
}
Avatar

Felix

October 19, 2007, October 19, 2007 16:07, permalink

1 rating. Login to rate!

The easy way.

1
2
3
4
5
<?php
function random_password($length = 8){
    return substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'),0,$length);
}
?>
Da8018e455a59bc1a8b5d40ff60f0966

Ted

October 26, 2007, October 26, 2007 12:06, permalink

1 rating. Login to rate!

The problem with these scripts above is that they don't produce a strong password. A password with a length of < 64 charachters will always have 0 or max 1 of each charachter. Passwords like 'aaBBcc' will never occure.

The code below is slower, but generates a more unique password.

1
2
3
4
5
6
7
8
function random_password($length = 6)
{
	$pass = '';
	for($n =0; $n < $length; $n++)
		$pass .= substr('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', rand(0, 63), 1);
	
	return $pass;
}
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

November 7, 2007, November 07, 2007 10:08, permalink

1 rating. Login to rate!

I'm using this function for random passing generation and I'm quite satisfied of it

Can you see any weakness?

1
2
3
4
function random_password($length = 6)
{
  return substr( md5(microtime()),0, $length);
}
C7a1b6cf2860b6ade63594b5a1e6af9c

Gerry

August 16, 2008, August 16, 2008 16:49, permalink

No rating. Login to rate!

Slight rework of Ted's code.

Main change is using mt_rand() instead of rand(), which is probably the cause of the speed issues which he mentioned.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
	function randomPassword($length = 6){
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
		$charsLen = strlen($chars)-1;
		$pass = '';
		
		for($n =0; $n < $length; $n++){
			$pass .= substr($chars, mt_rand(0, $charsLen), 1);
		}
		
		return $pass;
	}
?>
Fb222af1d07ca695061df966b1286164

Just Another Programmer

December 19, 2008, December 19, 2008 05:10, permalink

No rating. Login to rate!

This one uses special characters also, and shuffles it all around etc..

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
function randomPassword($length) {
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!@#$%^&*(){}[]|~';
    $charsarray = array();
    for ($n=0; $n < rand(0,rand(0,100)); $n++) {
        $charsarray[] = str_shuffle($chars);
    }
    $password = substr(str_shuffle(str_shuffle(implode($charsarray))),rand(0,strlen($password))-$length,$length);
    return $password;
}
echo randomPassword(12);
//Sample generated password: tY$%bSzzU!e[
?>

Your refactoring





Format Copy from initial code

or Cancel