Yes, Oracle generates random numbers using the built-in DBMS_RANDOM package, with the most direct call being DBMS_RANDOM.VALUE, which returns a random number between 0 and 1 (inclusive of 0, exclusive of 1). For integers, use DBMS_RANDOM.VALUE(low, high) with FLOOR to get a whole number in a range. This package is available in all supported Oracle Database editions and requires no special installation—just execute privileges on the package, which are granted to PUBLIC by default.
Background: Why Oracle Needs a Random Function
Random numbers in a database serve many purposes: generating test data, assigning sample rows for auditing, creating random passwords, shuffling query results, or adding jitter to scheduling. Oracle has provided DBMS_RANDOM since Oracle 8i, replacing the older DBMS_RANDOM.RANDOM function that returned signed 32-bit integers. The package is deterministic when seeded, meaning you can reproduce the same sequence—useful for repeatable tests—but by default it seeds from system entropy (time and session ID), so each call sequence is unpredictable.
How DBMS_RANDOM Works Under the Hood
DBMS_RANDOM uses a pseudo-random number generator (PRNG) based on a linear congruential algorithm. It maintains a state variable that is updated each call. The VALUE function returns a NUMBER with 38 digits of precision by default, but you can specify a smaller scale. The package also provides SEED to set the initial state, NORMAL to return numbers from a normal distribution (mean 0, standard deviation 1), and STRING to generate random strings from a character set (uppercase, lowercase, alphanumeric, etc.).
Important: DBMS_RANDOM is not cryptographically secure. For security-sensitive uses like session tokens or encryption keys, Oracle recommends DBMS_CRYPTO.RANDOMBYTES or the SYS_GUID() function, which draws from a stronger source. The Oracle documentation explicitly states that DBMS_RANDOM should not be used for security purposes.
How to Generate Random Numbers: Core Examples
Here are the most common patterns, with exact SQL syntax:
- Random decimal between 0 and 1:
SELECT DBMS_RANDOM.VALUE FROM dual;returns e.g., 0.73581234567890123456789012345678901234. - Random integer between 1 and 100:
SELECT FLOOR(DBMS_RANDOM.VALUE(1, 101)) FROM dual;Note the upper bound is exclusive, so you add 1 to include 100. - Random integer between low and high inclusive: Use
FLOOR(DBMS_RANDOM.VALUE(low, high+1)). For example,FLOOR(DBMS_RANDOM.VALUE(5, 11))gives 5 to 10. - Random date within a range:
SELECT TO_DATE('2024-01-01','YYYY-MM-DD') + DBMS_RANDOM.VALUE(0, 365) FROM dual;adds a random fraction of days. - Random string of 8 uppercase letters:
SELECT DBMS_RANDOM.STRING('U', 8) FROM dual;returns something like ‘KJHFGHTR’.
Choosing the Right Function for Your Use Case
Match the function to the job:
- Uniform decimal:
DBMS_RANDOM.VALUEwithout arguments for (0,1). - Uniform integer range:
FLOOR(DBMS_RANDOM.VALUE(low, high+1)). - Normal distribution:
DBMS_RANDOM.NORMALfor statistical sampling or simulation. - Random string:
DBMS_RANDOM.STRINGwith format ‘U’ (upper), ‘L’ (lower), ‘A’ (mixed case), ‘X’ (uppercase alphanumeric), ‘P’ (printable). - Cryptographic randomness:
DBMS_CRYPTO.RANDOMBYTES(n)returns raw bytes; useRAWTOHEXto convert. RequiresEXECUTEonDBMS_CRYPTO.
For a single random row from a table, you can use ORDER BY DBMS_RANDOM.VALUE but be aware that this sorts the entire table, which is inefficient for large tables. Better: use a sample clause like SELECT * FROM table SAMPLE(10) for approximate random sampling, or fetch a random primary key and then the row.
Common Problems and Pitfalls
Several issues trip up developers:
- Off-by-one errors: Forgetting that the upper bound is exclusive. To get 1 to 10, you must use
VALUE(1, 11). - Performance on large sets:
ORDER BY DBMS_RANDOM.VALUEon a million-row table can take seconds; it’s a full sort. UseSAMPLEor a two-step approach. - Seeding for reproducibility: If you need the same sequence each run, call
DBMS_RANDOM.SEED(42)before the first call. Without seeding, results are unpredictable. - Precision and rounding:
DBMS_RANDOM.VALUEreturns up to 38 digits; if you need fewer, useROUNDorTRUNC. - Security misuse: Using
DBMS_RANDOMfor passwords or tokens is a vulnerability. Always useDBMS_CRYPTOfor that.
Comparing DBMS_RANDOM with Other Oracle Randomness Tools
Oracle offers several ways to get randomness:
DBMS_RANDOM: Fast, easy, good for non-security uses.DBMS_CRYPTO.RANDOMBYTES: Cryptographically secure, returns RAW; slower but safe.SYS_GUID(): Returns a 16-byte globally unique identifier; not random in a statistical sense but unique. Useful for IDs, not for random numbers.DBMS_RANDOM.STRING: For strings, not numbers.DBMS_RANDOM.NORMAL: For Gaussian distribution.
For generating a large set of random numbers in a single query, you can use a CONNECT BY level trick: SELECT FLOOR(DBMS_RANDOM.VALUE(1, 101)) FROM dual CONNECT BY LEVEL <= 100; gives 100 random integers.
Costs and Availability
DBMS_RANDOM is part of the Oracle Database server, included in all editions (Express, Standard, Enterprise) with no additional license cost. It requires no setup. However, using it in PL/SQL or SQL is subject to normal CPU and I/O costs; generating millions of random numbers can be CPU-intensive. There is no limit on the number of calls, but the PRNG state is session-specific—each session has its own sequence. If you need a random number in a trigger or a stored procedure, it works fine.
Frequently Asked Questions
How do I get a random integer between 1 and 100 in Oracle? Use SELECT FLOOR(DBMS_RANDOM.VALUE(1, 101)) FROM dual; The upper bound 101 is exclusive, so the result is 1 to 100 inclusive.
Is DBMS_RANDOM secure for generating passwords? No. It is a pseudo-random generator, not cryptographically secure. Use DBMS_CRYPTO.RANDOMBYTES and encode it, e.g., UTL_RAW.CAST_TO_VARCHAR2(DBMS_CRYPTO.RANDOMBYTES(16)) or use the DBMS_RANDOM.STRING for non-security strings only.
How can I make the random sequence repeatable? Call EXEC DBMS_RANDOM.SEED(12345); before your first VALUE call in the same session. Then the sequence will be identical each time you run the same seed.
Why does ORDER BY DBMS_RANDOM.VALUE run slowly on a big table? Because it computes a random value for every row and then performs a full sort. For large tables, use SELECT * FROM table SAMPLE(0.1) for an approximate random sample, or fetch a random key and then the row.
Can I use DBMS_RANDOM in a SELECT statement without PL/SQL? Yes, it's a PL/SQL package but callable from SQL as shown in examples. Just ensure you have execute privilege, which is granted to public by default.
What's the difference between DBMS_RANDOM.VALUE and DBMS_RANDOM.NORMAL? VALUE returns a uniform distribution between 0 and 1 (or a specified range). NORMAL returns a value from a normal distribution with mean 0 and standard deviation 1, which can be negative and unbounded.
Related
- Alphalist Data Entry 7.4: How It Works
- Hourly Employee Shift Schedule Template Excel
- IMREAL Function in Google Sheets Explained
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Paste a Picture in Excel and Keep It in PlaceTo paste a picture into Excel for Microsoft 365 on Windows, copy the image itself, select the worksheet,…
- How to Delete Microsoft Excel Without Losing FilesTo delete Microsoft Excel from a Windows PC, open Settings > Apps > Installed apps, find Excel or…
- Formatting a Cell in Excel: The Complete GuideFormatting a cell in Excel means changing its appearance—font, color, borders, number format, alignment, and more—without altering the…
- Data Entry Operator WFH: Real Jobs, Pay & How to StartYes, you can work from home as a data entry operator, and it is one of the most…
- Freeze 2 Panes in Excel: What Works and What Doesn’tTo freeze two parts of a worksheet at once in Excel, select the cell just below the rows…
- Excel: Change European Number Format to US SeparatorsTo change Excel from European-style numbers such as 1.234,56 to US-style numbers such as 1,234.56 in Microsoft 365…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.