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.