I found this bit of PHP code to generate random strings (alphabetic, alphanumeric, numeric and hexadecimal).
<?php
function random($length = 8, $seeds = 'alpha') {
$seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
$seedings['numeric'] = '0123456789';
$seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
$seedings['hexidec'] = '0123456789abcdef';
if (isset($seedings[$seeds])) {
$seeds = $seedings[$seeds];
}
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
mt_srand($seed);
$str = '';
$seeds_count = strlen($seeds);
for ($i = 0; $length > $i; $i++) {
$str .= $seeds{mt_rand(0, $seeds_count - 1)};
}
return $str;
}
?>
If I run this function with default arguments (so it generates 8 lines of characters, only in alphabetical order) and generates 1,000,000 lines, I think my collision speed will be low:
26^8 = 208,827,064,576
1,000,000 / 208,827,064,576 ~= 0.0004%
In fact, when I run this on my machine, I get a 90% collision rate! Only 10% of my generated lines are unique.
Actually, this is suspiciously close to 10%. By creating multiple sets of 1,000,000 random strings, I find that each set generates ...
- 100,032 unique lines
- 100,035 unique lines
- 100,032 unique lines
- 100 028
- 100,030
- ...
, ? , , mt_srand, php mt_rand, - .
...
?
?