Created
November 4, 2014 14:50
-
-
Save AndreBaumeier/3cd529765061f14902a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* From http://rosettacode.org/wiki/Linear_congruential_generator#PHP | |
*/ | |
function bsd_rand($seed) { | |
return function() use (&$seed) { | |
return $seed = (1103515245 * $seed + 12345) % (1 << 31); | |
}; | |
} | |
/** | |
* From http://stackoverflow.com/questions/14556104/generating-a-unique-and-random-6-character-long-string-to-represent-link-in-ruby | |
*/ | |
$alphas = range('a', 'z'); | |
function encode_alphas($n, $alphas) { | |
$result = ''; | |
for ($i=0;$i<6;$i++) { | |
$m = $n % count($alphas); | |
$n = $n / count($alphas); | |
$result .= $alphas[$m]; | |
} | |
return $result; | |
} | |
$lcg = bsd_rand(0); | |
echo "BSD "; | |
for ($i = 0; $i < 10; $i++) | |
echo encode_alphas($lcg(), range('a', 'z')), " "; | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment