Created
April 30, 2012 14:40
-
-
Save julienfastre/2558868 to your computer and use it in GitHub Desktop.
test the risk of occurence of an uniqueid
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 | |
$a = array(); | |
class IdProvider { | |
private $n = 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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); | |
public function createId() { | |
$s = ''; | |
for ($i = 0; $i < 7; $i++) { | |
$o = array_rand($this->n); | |
$s .= $this->n[$o]; | |
} | |
return $s; | |
} | |
} | |
$s = new IdProvider(); | |
$collision = 0; | |
for ($i = 0; $i < 3000000; $i++) { | |
$id = $s->createId(); | |
//echo 'Nouveau slug : '.$id."\n"; | |
if (isset($a[$id])) { | |
$collision++; | |
echo 'une collision '.$id."\n"; | |
} else { | |
$a[$id] = $i; | |
} | |
} | |
echo "fin du script : $collision collisions \n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment