Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active December 20, 2015 06:29
Show Gist options
  • Save craiga/6085988 to your computer and use it in GitHub Desktop.
Save craiga/6085988 to your computer and use it in GitHub Desktop.
Get a unique random integer.
<?php
/**
* Get a unique random integer.
*
* @author Craig Anderson <craiga@craiga.id.au>
* @link https://gist.github.com/craiga/6085988
*/
function unique_rand($min = 0, $max = null) {
static $prev = array();
if(is_null($max)) {
$max = getrandmax();
}
do {
$n = rand($min, $max);
} while(in_array($n, $prev));
$prev[] = $n;
return $n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment