Skip to content

Instantly share code, notes, and snippets.

@samundra
Last active December 28, 2015 04:58
Show Gist options
  • Save samundra/7446003 to your computer and use it in GitHub Desktop.
Save samundra/7446003 to your computer and use it in GitHub Desktop.
<?php
function generateUsername() {
$small = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$capital = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
$numbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$f = rand(0, 25);
$s = rand(0, 25);
$firstPart = $small[$f] . $small[$s];
$secondPart = $capital[$f] . $capital[$s];
if (($f > 0 && $f < 10) && ($s > 0 && $s < 10)) {
$thirdPart = $numbers[$f] . $numbers[$s];
} else {
$f = rand(0, 9);
$s = rand(0, 9);
$thirdPart = $numbers[$f] . $numbers[$s];
}
return str_shuffle($firstPart . $secondPart . $thirdPart);
}
$sum = 0;
for ($i = 0; $i < 5000; $i++) {
ob_start();
$start = microtime();
echo generateUsername();
$end = microtime();
ob_end_clean();
//
$time = ($end - $start);
$sum += $time;
}
echo 'Total avg execution time : ' . ($sum/5000) . ' seconds';
?>
@samundra
Copy link
Author

Total execution time : 0.00027600000000005 seconds
Total execution time : 0.000524 seconds
Total execution time : 0.00027699999999997 seconds
Total execution time : 0.000274 seconds
Total execution time : 0.00027500000000003 seconds

@gcamrit
Copy link

gcamrit commented Dec 22, 2013

<?php
 function genUserName($length=6) {
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // add other character you want
return substr(str_shuffle(str_repeat($pool, 6)), 0, $length);
} 
?>

Easy to understand And Simple .... :P Hope It Works better For you Bro :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment