Last active
December 20, 2015 06:29
-
-
Save craiga/6085988 to your computer and use it in GitHub Desktop.
Get a unique random integer.
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 | |
/** | |
* 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