Who doesn't love a good UUID (Universally Unique Indentifier), used for a variety of purposes these days due to the VERY low chance of a duplicate appearing.

You can calculate the probability of a duplicate using the approximation below, where n = number of UUIDs and x = 2122

$$p(n) \approx 1 - e^{- \frac {n^2}{2x}} $$

LaTeX: p(n) \approx 1 - e^{- \frac {n^2}{2x}}

To put these numbers into perspective, one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion, that means the probability is about 0.00000000006 (6 x 10-11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.

Live Demo

Source Code


/*
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

<?php

function get_uuid() {
	return trim(`uuid -v4`);
}

if (isset($_POST['submit'])){

$number = trim($_POST['amount']);

	$start_time = microtime(true);
	
	if (ctype_digit($number) && $number <= 10) {
	
		// set counter variable
		$i = 1;
		
		echo "<p>Success!</p>\n";
		echo "<code>\n";
		
		while ($i <= $number) {
			echo get_uuid(). "<br />";
			$i++;
		}
		
		echo "</code>\n";
		
		$time_end = microtime(true);
		
		$time = $time_end - $start_time;
		echo "<p>Took " . $time . " seconds.";
		
	} else {
		echo "<p>It appears you either entered to high a number or not a number at all.</p>";
	} 	 
}

?>