若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1))。例如, 若要在7 到 12 的范围(包括7和12)内得到一个随机整数, 可使用以下语句:
SELECT FLOOR(7 + (RAND() * 6));
随机 (random)
Oracle 实现:
set serveroutput on
DECLARE
V_NUM NUMBER;
BEGIN
FOR I IN 1..40 LOOP
SELECT TRUNC(180+20*dbms_random.value) INTO V_NUM FROM dual;
DBMS_OUTPUT.put_line(V_NUM);
END LOOP;
END;
/
DECLARE
V_TIME_1 varchar2(30);
V_TIME_2 varchar2(30);
BEGIN
FOR I IN 1..20 LOOP
select '07'||':'||trunc(dbms_random.value(0,60)) into V_TIME_1 from dual;
select '17'||':'||trunc(dbms_random.value(30,60)) into V_TIME_2 from dual;
DBMS_OUTPUT.put_line(V_TIME_1);
DBMS_OUTPUT.put_line(V_TIME_2);
END LOOP;
END;
/
PS:
http://blog.csdn.net/feier7501/article/details/20802191 参考 oracle pl sql for循环