Skip to content

Instantly share code, notes, and snippets.

@julienfastre
Created April 30, 2012 14:40
Show Gist options
  • Save julienfastre/2558868 to your computer and use it in GitHub Desktop.
Save julienfastre/2558868 to your computer and use it in GitHub Desktop.
test the risk of occurence of an uniqueid
<?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