SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Quickly Generate Random number by TSQL with 99.9% Uniqueness Guarntee

By Namwar Rizvi,

There are many scenarios where we need Random numbers but the problem in generating a random number is to guarntee the uniqueness of it. There are lots of algorithms available to generate random numbers but each one of them has its own issues.

There is no algorithm which can give you 100% uniqueness guarntee for every new random number. Things get worst when we consider the RAND() function of SQL Server. This function will generate the same number again and again if it is in the same transaction.


To solve this, I use a quick and efficient technique to generate unique Random numbers with more than 99.9% uniqueness gurantee in millions of rows, even if they are generated in a single transaction.

Technique is very simple, use CHECKSUM() function and pass NEWID() function as a paremeter.


Select CheckSum(NewID()) as CS_RandomNumber

Since NewID() will always generate globally unique identifier so CheckSum() will always receive a new input therefore, it will always return (at least by definition in MSDN) a unique integer which is our target random number. I have tested it and found out that CheckSum() function can return the same number for two different inputs but it is a very very rare scenario. 99.9% of time, CheckSum() will return two different numbers for two different inputs.


How to Test

To test the technique, please execute the given TSQL script. This script will generate 3 million rows (in less than 10 seconds by using the technique as described in my blog here or here.

After generating 3 million rows, we execute two queries to show the list of duplicate random numbers generated by using CheckSum technique and normal RAND() function technique.


You will notice that CheckSum() technique will generate no more than 1000 random numbers in 3 million records which is just 0.0333 % of total records whereas RAND() function will generate the same number for all 3 million rows which kills the purpose of generating Random number altogether.

To test it further, you can change the value of @p_NumberOfRows variable. I have tested it for 100000 rows also and there was no duplicate.


I hope this simple and efficient technique will help you in solving the random number related problems.


Total article views: 2010 | Views in the last 30 days: 1
Related Articles

Random number generator

Random numbers


Generating Non-uniform Random Numbers with SQL

Some FUNCTIONs to help generate non-uniform random numbers from uniform random numbers including Nor...


How to Get Random Numbers

How to Get Random Numbers


Random Phone numbers

Can anyone help me generate a statement that will update a table with random phone numbers with the ...


Random Data Generator

A query to generate random values for several different data types.

random numbers