Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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.

Example:

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: 1977 | Views in the last 30 days: 7
 
Related Articles
FORUM

Random number generator

Random numbers

ARTICLE

Generating Non-uniform Random Numbers with SQL

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

FORUM

How to Get Random Numbers

How to Get Random Numbers

FORUM

Random Phone numbers

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

FORUM

random Number

Unique Number

Tags
random numbers    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones