62,243
社区成员




declare
type num_type is table of number;
num_arr num_type:=num_type();
n number:=0;
x number := 100;
begin
-- 循环处理20个数---------------------数自己改-------------------------------------------------------
for i in 1 ..20 loop
-- 扩展数组
num_arr.extend;
-- 如果是最后一个数,则不需要随机(因为和为100,所以只能随机19个数)
if i = 20 then
num_arr(i) := x;
-- 前19个数进行随机生成,因为要给后面的数保留值,所以只能在x - (20 -i) * 2中取值
else
select round(dbms_random.value(1,x - (20 - i)*2)) into num_arr(i) from dual;
end if;
-- 计算剩余的可取值范围
x := x - num_arr(i);
-- 输出显示
dbms_output.put_line(i||' '||num_arr(i));
end loop;
end;
user master
select sum(number) from(select top 100 number from spt_values where type='p' order by newid())t