To generate a random number in Oracle SQL, use the built-in function DBMS_RANDOM.VALUE, which returns a random number between 0 and 1 by default. For example, SELECT DBMS_RANDOM.VALUE FROM dual; returns a random decimal like 0.7342. You can also specify a range, such as DBMS_RANDOM.VALUE(1, 100) for a random number between 1 and 100 (inclusive of the lower bound, exclusive of the upper bound).
Why Oracle Doesn’t Have a Simple RAND() Function
Unlike MySQL or PostgreSQL, Oracle does not provide a standalone RAND() function in SQL. Instead, Oracle bundles random number generation into the DBMS_RANDOM package, which is part of the database’s PL/SQL utility library. This package has been available since Oracle 8i and is the standard, documented way to produce random values in both SQL and PL/SQL. The reason for this design is that Oracle separates low-level utilities into packages to keep the core SQL language lean and to allow for more advanced features like seed control and multiple distribution types.
Using DBMS_RANDOM.VALUE: Syntax and Examples
The most common usage is DBMS_RANDOM.VALUE with optional low and high arguments. Here are concrete examples you can run in SQL*Plus, SQL Developer, or any Oracle SQL client:
SELECT DBMS_RANDOM.VALUE FROM dual;– returns a random number between 0 and 1, e.g., 0.4521.SELECT DBMS_RANDOM.VALUE(1, 10) FROM dual;– returns a random number between 1 and 10, e.g., 7.3821. The lower bound is inclusive, the upper bound is exclusive.SELECT ROUND(DBMS_RANDOM.VALUE(1, 100), 0) FROM dual;– returns a random integer between 1 and 99 (because of rounding and the exclusive upper bound).SELECT FLOOR(DBMS_RANDOM.VALUE(1, 101)) FROM dual;– returns a random integer between 1 and 100 inclusive, because the upper bound is exclusive, so 101 is never reached and FLOOR gives 1-100.
To generate a random integer in a specific range, the common pattern is FLOOR(DBMS_RANDOM.VALUE(low, high + 1)). For example, to get a random integer between 5 and 15 inclusive: SELECT FLOOR(DBMS_RANDOM.VALUE(5, 16)) FROM dual;.
Other DBMS_RANDOM Functions: STRING, NORMAL, and SEED
The DBMS_RANDOM package includes more than just VALUE. You can generate random strings with DBMS_RANDOM.STRING, which takes a format character and a length. For instance, DBMS_RANDOM.STRING('U', 10) returns 10 uppercase letters, 'L' for lowercase, 'A' for mixed-case letters, and 'X' for uppercase letters and digits. To generate a random number following a normal distribution, use DBMS_RANDOM.NORMAL, which returns a random number from a standard normal distribution (mean 0, standard deviation 1). You can also control reproducibility by setting a seed with DBMS_RANDOM.SEED. For example, EXEC DBMS_RANDOM.SEED(42); will make subsequent calls generate the same sequence, which is useful for testing and debugging.
How to Choose the Right Random Number Method
Choosing the right approach depends on what you need:
- Random decimal between 0 and 1: use
DBMS_RANDOM.VALUEwith no arguments. - Random decimal in a range: use
DBMS_RANDOM.VALUE(low, high). - Random integer in a range: use
FLOOR(DBMS_RANDOM.VALUE(low, high + 1)). - Random string: use
DBMS_RANDOM.STRING(format, length). - Random date: combine
DBMS_RANDOM.VALUEwith date arithmetic, e.g.,SYSDATE + DBMS_RANDOM.VALUE(1, 365)for a random date within the next year. - Random row from a table: use
ORDER BY DBMS_RANDOM.VALUEto shuffle rows, but be aware this can be slow on large tables.
For most applications, DBMS_RANDOM.VALUE is sufficient. If you need cryptographically secure random numbers, Oracle also provides DBMS_CRYPTO.RANDOMBYTES, but that is more complex and usually reserved for security-sensitive tasks like generating encryption keys.
Common Problems and Misconceptions
One frequent mistake is expecting DBMS_RANDOM.VALUE(1, 10) to return integers. It returns decimals, so you must use FLOOR or ROUND to get whole numbers. Another issue is the exclusive upper bound: many users assume both bounds are inclusive, but only the lower bound is. For example, DBMS_RANDOM.VALUE(1, 10) can return 1.000 but never 10.000. A third misconception is that calling DBMS_RANDOM.VALUE in a query with a WHERE clause will generate a new random number for each row. In Oracle, if you use a function in a SELECT list, it is evaluated once per row, which is usually what you want. However, if you use it in a WHERE clause, Oracle may evaluate it once for the entire statement, leading to unexpected results. For example, WHERE id = FLOOR(DBMS_RANDOM.VALUE(1, 100)) might evaluate the random number only once, returning one random row, not a random selection per row. To avoid this, use a subquery or ORDER BY DBMS_RANDOM.VALUE with FETCH FIRST 1 ROW ONLY.
Performance and Efficiency Considerations
Using DBMS_RANDOM.VALUE in a large query can have performance implications. The function is relatively fast, but if you call it millions of times, it adds CPU overhead. For generating a large set of random numbers, consider using a recursive CTE or a row generator, but the simplest method is often just to use the function in a SELECT from dual with a CONNECT BY to generate many rows. For example, to generate 100 random numbers: SELECT DBMS_RANDOM.VALUE FROM dual CONNECT BY LEVEL <= 100;. This is efficient and widely used. When ordering a large table randomly, ORDER BY DBMS_RANDOM.VALUE is notoriously slow because it requires a full sort. For picking a single random row from a large table, a better approach is to use a query like SELECT * FROM (SELECT * FROM table ORDER BY DBMS_RANDOM.VALUE) WHERE ROWNUM = 1, but that still sorts. A more efficient method is to use a subquery that selects a random row number using ROW_NUMBER() and then pick that row, but that also requires scanning. For very large tables, consider using a random primary key or a precomputed random column.
Alternatives: SYS_GUID and Other Methods
If you need a random hexadecimal string, you can use SYS_GUID(), which returns a 32-character globally unique identifier. While not a numeric random number, it is often used as a surrogate key. To convert it to a number, you could use TO_NUMBER(SUBSTR(SYS_GUID(), 1, 8), 'XXXXXXXX'), but that is not a uniform random number. Another alternative is to use DBMS_RANDOM.VALUE in PL/SQL, where you can assign it to a variable. For example, v_rand := DBMS_RANDOM.VALUE(1, 100);. In Oracle 18c and later, you can also use the DBMS_RANDOM package in SQL with the WITH clause, but there is no native SQL-only random function. If you are using Oracle Database 12c or later, you can use the DBMS_RANDOM package in a PL/SQL function and call it from SQL, but that is overkill for most cases.
Frequently Asked Questions
How do I generate a random integer between 1 and 100 in Oracle?
Use FLOOR(DBMS_RANDOM.VALUE(1, 101)). The VALUE function returns a decimal between 1 (inclusive) and 101 (exclusive), so FLOOR gives you integers from 1 to 100. Alternatively, you can use ROUND(DBMS_RANDOM.VALUE(1, 100), 0), but that can return 100 only if the decimal is 99.5 or higher, and it also can return 0 if the value is less than 0.5, so the FLOOR method is safer.
Can I use DBMS_RANDOM.VALUE in a WHERE clause?
Yes, but be careful. Oracle may evaluate the function once for the entire statement, not per row, depending on the optimizer. For example, WHERE id = FLOOR(DBMS_RANDOM.VALUE(1, 100)) might return zero or one row, not a random row per row. To get a random row, use ORDER BY DBMS_RANDOM.VALUE with FETCH FIRST 1 ROW ONLY (Oracle 12c+) or WHERE ROWNUM = 1 in a subquery.
Is DBMS_RANDOM.VALUE cryptographically secure?
No, it is not. DBMS_RANDOM uses a pseudo-random number generator that is not suitable for security-sensitive applications like generating passwords or encryption keys. For secure random numbers, use DBMS_CRYPTO.RANDOMBYTES or DBMS_CRYPTO.RANDOMINTEGER, which use a cryptographic random number generator.
How do I generate a random date in Oracle?
You can add a random number of days to a base date. For example, to get a random date in the next year: SELECT SYSDATE + DBMS_RANDOM.VALUE(1, 365) FROM dual;. To get a random date between two specific dates, use TO_DATE('2025-01-01', 'YYYY-MM-DD') + DBMS_RANDOM.VALUE(0, TO_DATE('2025-12-31', 'YYYY-MM-DD') - TO_DATE('2025-01-01', 'YYYY-MM-DD')).
Why does DBMS_RANDOM.VALUE(1, 10) sometimes return 10?
It never returns exactly 10 because the upper bound is exclusive. The function returns a value greater than or equal to 1 and less than 10. So the maximum possible value is 9.99999... If you use ROUND, it could round up to 10, but that is a rounding artifact. To get integers from 1 to 10 inclusive, use FLOOR(DBMS_RANDOM.VALUE(1, 11)).
Can I use DBMS_RANDOM.VALUE in an INSERT statement?
Yes, you can use it to insert random values into a table. For example, INSERT INTO t (id, rand_num) VALUES (1, DBMS_RANDOM.VALUE(1, 100));. If you want to insert a random value for each row in a multi-row insert, you can use a subquery with CONNECT BY or a SELECT from a table with many rows.
Related
- Shortcut to Exit Window? Quick Keys & Fixes
- Data Entry and Data Management: Clear Answers
- Google Sheets Percentage of Total: Easy Formula
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Best Monthly Budget Spreadsheet: 5 Options ComparedFor most people who want a straightforward monthly budget spreadsheet, start with the personal budget templates available in…
- Google Sheets Date Format: How to Turn It On and Set ItTo turn on date formatting in Google Sheets, select the cells you want to format, then choose Format…
- Merge Cells in Google Sheets: Steps and Data TipsTo merge cells in Google Sheets in a web browser, select the cells, then choose Format > Merge…
- Excel Row Limit: How Many Rows Can a Worksheet Hold?In Microsoft Excel for Microsoft 365, a worksheet can hold a maximum of 1,048,576 rows. That is the…
- Excel Named Ranges: How to Reference ThemTo refer to a named range in an Excel formula, use its name where you would otherwise use…
- How to Insert a Line in Word: Borders and Shapes GuideTo insert a line in a Word document, type three hyphens (—) on a blank line and press…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.