范围内的随机整数
来自 PostgreSQL wiki
跳转到导航跳转到搜索
CREATE OR REPLACE FUNCTION pg_round_random_range(int, int) RETURNS integer
LANGUAGE plperl IMMUTABLE RETURNS NULL ON NULL INPUT
AS $pg_round_random_range$
my($imin, $imax) = @_;
if ($_[0] == $_[1]){
return $_[0]; }
if($imin > $imax){
$imin = $_[1];
$imax = $_[0]; }
$_number_ = $imin + ( (rand) * ($imax -$imin + 1));
return sprintf "%d",$_number_;
$pg_round_random_range$;
如果你不想控制参数
CREATE OR REPLACE FUNCTION pg_round_random_range(int, int) RETURNS integer
LANGUAGE plperl IMMUTABLE RETURNS NULL ON NULL INPUT
AS $pg_round_random_range$
my($imin, $imax) = @_;
$_number_ = $imin + ( (rand) * ($imax -$imin + 1));
return sprintf "%d",$_number_;
$pg_round_random_range$;
- 3manuek