Random Number SQL Server: RAND, NEWID, and CRYPT_GEN_RANDOM

How-to basics · Updated November 23, 2026

The simplest way to generate a random number in SQL Server is with the RAND() function, but for truly random values or random ordering in a query, you need NEWID() or CRYPT_GEN_RANDOM(). This article explains how each works, when to use them, and common pitfalls.

Background: Why Random Numbers Are Tricky in SQL

SQL Server is a set-based language, so generating random numbers row-by-row isn’t as straightforward as in procedural languages. The RAND() function returns a float between 0 and 1, but it is evaluated once per query unless you provide a seed. For example, SELECT RAND() gives one value, but SELECT RAND() FROM sys.objects returns the same value for every row because the function is called once and reused. To get a different random number per row, you need a different approach, like using NEWID() or a seed based on a column.

RAND(): The Basic Random Float

RAND() is the simplest function. It returns a float from 0 (inclusive) to 1 (exclusive). You can provide an optional integer seed: RAND(seed) produces a repeatable sequence if the same seed is used. Without a seed, SQL Server uses a random seed based on the system clock, so consecutive calls give different values.

To get an integer in a range, use FLOOR(RAND() * (max - min + 1)) + min. For example, a random integer between 1 and 10:

SELECT FLOOR(RAND() * 10) + 1;

This works for a single value, but if you need a random number per row in a result set, RAND() alone won’t do it. You can force a per-row seed using a column, like RAND(ROW_NUMBER() OVER (ORDER BY ...)), but that’s clunky and not truly random.

NEWID(): Random GUIDs and Ordering

NEWID() generates a unique identifier (GUID) that is statistically random. You can use it to randomize row order: ORDER BY NEWID(). This is the classic way to select random rows from a table. For example, to get 5 random rows from a table:

SELECT TOP 5 * FROM Employees ORDER BY NEWID();

To convert a GUID to a random number, you can use CHECKSUM(NEWID()), which returns an integer. But be careful: CHECKSUM can return negative values, and it is not uniformly distributed. A common trick for a random integer between 1 and N is:

SELECT ABS(CHECKSUM(NEWID())) % N + 1;

However, this has a slight bias if N does not divide the range of CHECKSUM evenly. For most practical purposes, it’s fine, but for cryptographic applications, use CRYPT_GEN_RANDOM.

CRYPT_GEN_RANDOM(): Cryptographically Secure Random Numbers

If you need a random number for security-sensitive purposes (like generating a token or salt), use CRYPT_GEN_RANDOM(). It generates a cryptographically strong random number of a specified length in bytes. For example, to get a 4-byte random number (which can be up to 2^32 – 1):

SELECT CRYPT_GEN_RANDOM(4);

This returns a varbinary value. To convert it to an integer, you can use CONVERT(INT, CRYPT_GEN_RANDOM(4)), but be aware that the result can be negative. To get a positive integer in a range, you can use ABS(CONVERT(INT, CRYPT_GEN_RANDOM(4))) % N + 1, but again, there is a tiny bias. For a truly uniform distribution, you would need to use rejection sampling, which is more complex.

How to Choose: Which Function to Use?

Here’s a quick guide:

  • Single random float: RAND().
  • Random row ordering: ORDER BY NEWID().
  • Random integer per row (non-secure): ABS(CHECKSUM(NEWID())) % N + 1.
  • Cryptographically secure random bytes: CRYPT_GEN_RANDOM().
  • Repeatable random sequence (e.g., for testing): RAND(seed).

For most applications, NEWID() is the go-to for random selection because it’s simple and works per row. CRYPT_GEN_RANDOM is overkill unless you’re dealing with passwords, tokens, or other security-sensitive data.

Common Problems and Misconceptions

One common mistake is using RAND() in a WHERE clause expecting a different value per row. For example, SELECT * FROM Orders WHERE OrderID = FLOOR(RAND() * 1000) will evaluate RAND() once, not per row, so it might not match any row. Another misconception is that NEWID() is slow; it’s actually quite fast for typical row counts.

Another issue: ORDER BY NEWID() scans the entire table and sorts, which can be expensive on large tables. For large tables, consider using a random number column or a more efficient method like pulling a random key range.

Alternatives and Performance Considerations

If you need random numbers in T-SQL loops, you can use RAND() with a changing seed, but for set-based operations, NEWID() is better. In SQL Server 2008 and later, you can also use CRYPT_GEN_RANDOM. For generating random dates, you can combine DATEADD with a random number. For example, a random date in 2024:

SELECT DATEADD(day, ABS(CHECKSUM(NEWID())) % 366, '2024-01-01');

Performance-wise, ORDER BY NEWID() requires a sort, so on a table with millions of rows, it can be slow. An alternative is to use a pre-generated random number column with an index, or use a sampling method like TABLESAMPLE, but that doesn’t give a truly random sample without replacement.

Frequently Asked Questions

How do I generate a random number between 1 and 100 in SQL Server?

Use SELECT FLOOR(RAND() * 100) + 1; for a single value. For per-row, use SELECT ABS(CHECKSUM(NEWID())) % 100 + 1 FROM YourTable;

Why does RAND() return the same value for every row?

Because RAND() is evaluated once per query execution, not per row. To get per-row randomness, use NEWID() or provide a seed that changes per row.

Can I use NEWID() to generate a random integer?

Yes, by converting the GUID to a checksum: ABS(CHECKSUM(NEWID())). But for cryptographic security, use CRYPT_GEN_RANDOM.

Is ORDER BY NEWID() efficient for large tables?

No, it requires sorting the entire table. For large tables, consider alternative methods like selecting a random key range or using a pre-generated random column.

What is the difference between RAND() and CRYPT_GEN_RANDOM()?

RAND() is a pseudo-random number generator that is fast but not cryptographically secure. CRYPT_GEN_RANDOM() uses the operating system’s cryptographic random number generator, making it suitable for security-sensitive data.

Can I generate a random decimal number between two values?

Yes, use RAND() and scale it: SELECT RAND() * (max - min) + min; For example, between 5 and 10: SELECT RAND() * 5 + 5;

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.