DISQUS

Eddie Awad’s Blog: Random string generator

  • Robert Vollman · 4 years ago
    Speaking of random numbers, what do you think about this problem of mine:

    http://thinkoracle.blogspot.com/2005/05/random-...
  • Eddie Awad · 4 years ago
    Robert,
    I could not understand your problem. However, here are some clarifications about the package DBMS_RANDOM which I'm sure you already know, but does not hurt to add them here for future reference:

    The package DBMS\_RANDOM is only available in Oracle version 8 and above.

    If you want to encrypt sensitive data, then you should use the built-in package DBMS\_OBFUSCATION\_TOOLKIT, not DBMS\_RANDOM .

    For an analysis of DBMS\_RANDOM, how it works and the quality of the generated random numbers or strings, I suggest this article.

    The following functions are not documented in Oracle 8i and Oracle 9i DBMS\_RANDOM's documentation, but they are in Oracle 10g's: NORMAL, VALUE and STRING. A look at the source code of DBMS\_RANDOM reveals these hidden and useful functions.
  • edcas · 4 years ago

    Hi Eddie,


    I can't really figure out what's the use for SEED procedure here. Can you explain further?


    As I execute the code below, it give me the same result.


    <pre>
    BEGIN
    dbms_random.seed(1234535678);
    dbms_output.put_line('Random1: '||dbms_random.value);
    END;

    Random1: .62004818671882084550610341425311827476
    </pre>
  • Eddie Awad · 4 years ago

    edcas, the seed is needed to generate the random number or string. The seed can be a number or a string. If you do not initialize the generator by explicitly calling the seed procedure, a default seed is used, in that case, it is equal to:


    <pre>
    TO_CHAR(SYSDATE,'MM-DD-YYYY HH24:MI:SS') ||
    USER || USERENV('SESSIONID')
    </pre>

    So, if you do not seed, and then call dbms_random.value, chances are you will not get the same random value.


    The dbms_random package specification says: If this package is seeded twice with the same seed, then accessed in the same way, it will produce the same results in both cases.


    So, your observation is correct, given the same seed over and over again, you will get the same result each time you execute the same dbms_random procedure.


    How can a seed be used? well, one example is when you want to generate a random password. You would seed the generator with the username, so distinct usernames will have distinct random passwords, but same usernames will have the same random password since the seed (username) is the same.