1

I can't seem to use DBMSRAND.

GRANT EXECUTE ON DBMS_RANDOM TO *schema*;

ORA-01031: insufficient priveleges

Not sure why as I'm using the DBadmin schema to grant, but does anyone have a work around for random number generation in oracle sql?

4
  • 1
    just connect to sys as sysdba or system users, and then grant as in the question. Commented Nov 2, 2020 at 17:42
  • 2
    Just to expand a bit on @BarbarosÖzhan, the error message means exactly what it says. Instead of looking for some work-around, you should address the actual issue. The 'insufficient privilege' means the user issuing the GRANT does not have the privilege to issue the GRANT. I suspect you were connected as the user you wanted to grant it to, but allowing that would be a security hole big enough to drive a Mac truck through. Commented Nov 2, 2020 at 18:30
  • 1
    You could use to_char(SYSTIMESTAMP, 'FF') as SYSTIMESTAMP has resolution of nanoseconds (depending on your DB server). But for sure, the proper way of doing is: ask your DBA to get EXECUTE privileges on DBMS_RANDOM. Commented Nov 2, 2020 at 18:58
  • Try this if it helps you till you get the access .... stackoverflow.com/questions/30605545/… Commented Nov 2, 2020 at 19:53

1 Answer 1

1

If you're really desperate you could roll your own, eg here's an LG one

SQL> create or replace
  2  package global is
  3    a number := to_number(to_char(systimestamp,'FF'));
  4  end;
  5  /

Package created.

SQL>
SQL> create or replace
  2  function rand(r in number) return number is
  3
  4    mill  constant  number:=100000000;
  5    ten_thou constant number:=10000;
  6    lg_num  constant  number:=31415821;
  7
  8    v number;
  9    w number;
 10    x number;
 11    y number;
 12    z number;
 13
 14  begin
 15    w:=trunc(global.a/ten_thou);
 16    x:=mod(global.a,ten_thou);
 17    y:=trunc(lg_num/ten_thou);
 18    z:=mod(lg_num,ten_thou);
 19    v := mod((mod(x*y+w*z,ten_thou)*ten_thou+x*z),mill);
 20    global.a:=mod(v+1,mill);
 21    return(trunc((trunc(global.a/ten_thou)*r)/ten_thou));
 22  end;
 23  /

Function created.

SQL>
SQL> select rand(100) from dual;

 RAND(100)
----------
        21

SQL> select rand(100) from dual;

 RAND(100)
----------
        72

SQL> select rand(100) from dual;

 RAND(100)
----------
         1

SQL> select rand(100) from dual;

 RAND(100)
----------
        43

but random number generation is a science so you should be aware of the all of the limitations and drawbacks of rolling your own.

https://en.wikipedia.org/wiki/Linear_congruential_generator

So your best bet is to get appropriate access to the appropriate packages.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.