PLSQL generate random integer

后端 未结 6 1178
离开以前
离开以前 2021-02-08 11:53

In Oracle Sql developer 11g, how do I generate a random integer and assign it to a variable? This is what I\'ve tried so far:

S_TB := SELECT dbms_random.value(1,         


        
6条回答
  •  孤街浪徒
    2021-02-08 12:48

    For a set of consecutive integers randomly distributed uniformly (in the example below between 1 and 10), I suggest:

    select round(dbms_random.value(0.5,10.49999999999),0) from dual
    

    Otherwise I'll unintentionally restrict the first and last number in the set to half the probability of being chosen as the rest of the set.

    As pointed out by q3kep and GolezTrol, dbms_random.value(x, y) provides a uniform random distribution for values greater than or equal to x and less than y. So either of the following would be appropriate:

    select trunc(dbms_random.value(1,11)) from dual
    

    or

    select round(dbms_random.value(0.5, 10.5), 0) from dual
    

    As per documentation

提交回复
热议问题