20 Digit Random Key Generator
Random String Generator. This form allows you to generate random text strings. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
By: Aaron Bertrand Updated: 2013-09-17 Comments (20) Related: More >T-SQL
- random 16 digit generator What is an example of a strong password With strong 16 digit generator or secure 16 digit generator? Online 16 digit generator A strong password describes a password that is difficult to detect by both humans and computer programs, effectively protecting data.
- Produce a random alphanumeric string from the English alhpabet or any other alphabet of your choosing (custom input). The alphanumeric strings can be from any alphabet (German, French, Spanish, Russian, etc.). The random string generator can produce random alphanumeric strings of any required length and is helpful for randomly generating tokens for a raffle.
- The form below is a random string generator, which can be utilized to generate a series of coupon codes, unique passwords and any other random alphanumeric strings. By entering 'L' for Letters, 'N' for Numbers and 'S' for Symbols, the string generator can create strings that are more than 10 characters in length.
- The generator is easy, 100% safe and secure and you don't have to worry while using it. A generator is a tool used for producing unique codes that can be used on the Xbox store. Our Xbox gift card generator tools generate $25, $50, $100 gift card.
- To create a random 3-digit number between 100 and 999, use the following function: =RANDBETWEEN(100,999) To create a random letter, use the following function: =CHAR(RANDBETWEEN(65,90)) (Note: in ASCII 65 is A and 90 is Z) To create a random alphanumeric string that has two letters and then two numbers, use.
- The form below is a random string generator, which can be utilized to generate a series of coupon codes, unique passwords and any other random alphanumeric strings. By entering 'L' for Letters, 'N' for Numbers and 'S' for Symbols, the string generator can create strings that are more than 10 characters in length.
Problem
From time to time, I see a requirement to generate random identifiers for things like users or orders. People want to use random numbers so that the 'next' identifier is not guessable, or to prevent insight into how many new users or orders are being generated in a given time frame. They could use NEWID() to solve this, but they would rather use integers due to key size and ease of troubleshooting.
Let's say we want all users to have a random number between 1,000,000 and 1,999,999 - that's a million different user IDs, all 7 digits, and all starting with the number 1. We may use one of these calculations to generate a number in this set:
(These are just quick examples - there are probably at least a dozen other ways to generate a random number in a range, and this tip isn't about which method you should use.)
These seem to work great at the beginning - until you start generating duplicates. Even when you are pulling from a pool of a million numbers, you're eventually going to pull the same number twice. And in that case, you have to try again, and sometimes try again multiple times, until you pull a number that hasn't already been used. So you have to write defensive code like this: Download video card driver for hp laptop.
Never mind that this is really ugly, and doesn't even contain any transaction or error handling, this code will logically take longer and longer as the number of 'available' IDs left in the range diminishes.
Solution
One idea I've had to 'solve' this problem is to pre-calculate a very large set of random numbers; by paying the price of storing the numbers in advance, we can guarantee that the next number we pull won't have already been used. All it requires is a table and some code to pull the next number from the set. One way to populate such a table:
This took about 15 seconds to populate on my system, and occupied about 20 MB of disk space (30 MB if uncompressed). I'll assume that you have 20 MB of disk and memory to spare; if you don't, then this 'problem' is likely the least of your worries. :-)
Now, in order to generate the next ID, we can simply delete the lowest RowNumber available, and output its NextID for use. We'll use a CTE to determine the TOP (1) row so that we don't rely on 'natural' order - if you add a unique constraint to NextID, for example, the 'natural' order may turn out to be based on that column rather than RowNumber. We'll also output the result into a table variable, rather than insert it directly into the Users table, because certain scenarios - such as foreign keys - prevent direct inserts from OUTPUT.
When we come close to exhausting the first million values (likely a good problem), we can simply add another million rows to the table (moving on to 2,000,000 to 2,999,999), and so on. It may be wise to set up some automation to periodically checking how many rows are left, so that you can re-populate well in advance of actually running out of numbers.
Performance Metrics for Generating Random Values in SQL Server
I ran both methods 1,000,000 times, filling the Users table up with these random UserID values. The following chart shows that, while generating a random number at runtime is faster out of the gates, the cost of duplicate checking (and retrying in the event of a collision) quickly overtakes the read cost of the predefined table, and grows rapidly and eventually exponentially as more and more values are used up:
In the first 1,000 inserts, there were zero collisions. In the last 1,000 inserts, the average collision count was over 584,000. This, of course, is a problem that doesn't occur when you *know* that the next number you pull can't possibly be a duplicate (unless someone has populated the Users table through some other means).
Conclusion
We can trade a bit of disk space and relatively predictable (but not optimal) performance for the guarantee of no collisions, no matter how many random numbers we've already used. This doesn't seem like a good trade in the early going, but as the number of ID values used increases, the performance of the predefined solution does not change, while the random numbers generated at runtime really degrades performance-wise as more and more collisions are encountered.
Next Steps

- I encourage you to perform your own testing to see if a predefined set of random numbers might make more sense in your environment.
- Review the following tips and other resources:
- Documentation for CRYPT_GEN_RANDOM(), CHECKSUM(), NEWID() and RAND().
Last Updated: 2013-09-17
About the author
Random 3 Digit Generator
View all my tips