Talk:Random integer in a range

From PostgreSQL wiki

Jump to: navigation, search

--Lazzy 03:20, 2 July 2009 (UTC): I find it simpler to use the following segment:

$_number_ = $imin + ( (rand) * ($imax -$imin + 1));
return sprintf "%d",$_number_;

because it correctly returns the lower bound when (if) rand returns 0, and removes the need for a while() statement.

If it is desired to not have the lower bound returned, then simply send it with 1 unit bigger (ex: (min+1, max) ).

Personal tools