0

I want to generate random Numbers in Oracle 11g. But due to client request, I cannot use dbms_random.random of Oracle.

Is there any other way I can generate random numbers?

Duplication are strictly not allowed, numbers should be of length 4

0001
0053
9871
0098
0003

If any other Oracle library from which I can generate, then suggestions would be appreciated :)

9
  • 1
    Please explain why you cannot use dbms_randomrandom(). That does not seem like a reasonable limitation. Commented Nov 30, 2014 at 13:06
  • Client Has Said not to use it .. And you know client/Customer is the real boss :) Commented Nov 30, 2014 at 13:16
  • Thats why i mentioned it in my question .. if there is any other way then it will be a real help for me !! Commented Nov 30, 2014 at 13:17
  • 2
    Without knowing the reason why, it is impossible to say if another solution would meet the requirement. Commented Nov 30, 2014 at 13:23
  • Sir , I want a solution , if there is any solution then ur help will be appreciated .. Commented Nov 30, 2014 at 14:05

3 Answers 3

1

If you need to select a four digit number without duplication, you can start by creating a table of numbers from 0 to 9999:

create table numbers as 
    select level - 1 as seqnum
    from dual
    connect by level <= 10000;

Then order this randomly and choose however many non-repeating numbers you want. Up to 10,000 of course. I think this will work:

with random as (
      select to_char(n.seqnum, '0000') as val, row_number() over (order by sys_guid) as seqnum
      from numbers
     )
select *
from random
where seqnum <= 10;
Sign up to request clarification or add additional context in comments.

2 Comments

I'll do convey this to my managers .. and will come back to you if i need more assistance in this regard :)
Because from the look of it .. i think this is the ONLY solution to my question besides dbms_random ( which is not required ) ..
1

If you do not want to use dbms_random and your number must be distinct you can use pure sequence with a little help of "Fermat's little theorem".

See: http://en.wikipedia.org/wiki/Fermat%27s_little_theorem

Simply use sequence number, put into the formula and you get "semi-random" unique number. Search wikipedia about "Discrete logarithm". The same principle is also used for OTP (one time passwords), if the sequence number is decreasing.

Comments

1

u can use milliseconds from SYSTIMESTAMP

select to_char(systimestamp,'ff')/1000 from dual

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.