<p>A few months back I was tasked with implementing a unique and random code for our web application.  The code would have to be user friendly and as small as possible, but still be essentially random (so users couldn't easily predict the next code in the sequence).</p>

<p>It ended up generating values that looked something like this:</p>

<pre><code>Af3nT5Xf2
</code></pre>

<p>Unfortunately, I was never satisfied with the implementation.  Guid's were out of the question, they were simply too big and difficult for users to type in.  I was hoping for something more along the lines of 4 or 5 characters/digits, but our particular implementation would generate noticeably patterned sequences if we encoded to less than 9 characters.</p>

<p>Here's what we ended up doing:</p>

<p>We pulled a unique sequential 32bit id from the database.  We then inserted it into the center bits of a 64bit RANDOM integer.  We created a lookup table of easily typed and recognized characters (A-Z, a-z, 2-9 skipping easily confused characters such as L,l,1,O,0, etc.).  Finally, we used that lookup table to base-54 encode the 64-bit integer.  The high bits were random, the low bits were random, but the center bits were sequential.</p>

<p>The final result was a code that was much smaller than a guid and looked random, even though it absolutely wasn't.</p>

<p>I was never satisfied with this particular implementation.  What would you guys have done?</p>

[{'answer_id': 34722, 'author': 'Anthony Mastrean', 'author_id': 3619, 'author_profile': 'https://Stackoverflow.com/users/3619', 'pm_score': 2, 'selected': False, 'text': '<p>In C#, I have used the \'<a href="http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx" rel="nofollow noreferrer">System.IO.Path.GetRandomFileName() : String</a>\' method... but I was generating salt for debug file names. This method returns stuff that looks like your first example, except with a random \'.xyz\' file extension too.</p>\n\n<p>If you\'re in .NET and just want a simpler (but not \'nicer\' looking) solution, I would say this is it... you could remove the random file extension if you like.</p>\n'}, {'answer_id': 34727, 'author': 'Joshua Turner', 'author_id': 820, 'author_profile': 'https://Stackoverflow.com/users/820', 'pm_score': 3, 'selected': False, 'text': '<p>In .NET you can use the RNGCryptoServiceProvider method GetBytes() which will "fill an array of bytes with a cryptographically strong sequence of random values" (from ms documentation).</p>\n\n<pre><code>byte[] randomBytes = new byte[4];\nRNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();\nrng.GetBytes(randomBytes);\n</code></pre>\n\n<p>You can increase the lengh of the byte array and pluck out the character values you want to allow.</p>\n'}, {'answer_id': 34736, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s how I would do it.</p>\n\n<p>I\'d obtain a list of common English words with usage frequency and some grammatical information (like is it a noun or a verb?). I think you can look around the intertubes for some copy. Firefox is open-source and it has a spellchecker... so it must be obtainable somehow.</p>\n\n<p>Then I\'d run a filter on it so obscure words are removed and that words which are too long are excluded.</p>\n\n<p>Then my generation algorithm would pick 2 words from the list and concatenate them and add a random 3 digits number.</p>\n\n<p>I can also randomize word selection pattern between verb/nouns like</p>\n\n<blockquote>\n  <p><strong>eatCake778<br>\n  pickBasket524<br>\n  rideFlyer113\n  etc..</strong></p>\n</blockquote>\n\n<p>the case needn\'t be camel casing, you can randomize that as well. You can also randomize the placement of the number and the verb/noun.</p>\n\n<p>And since that\'s a lot of randomizing, Jeff\'s <a href="http://www.codinghorror.com/blog/archives/001015.html" rel="noreferrer">The Danger of Naïveté</a> is a must-read. Also make sure to study dictionary attacks well in advance.</p>\n\n<p>And after I\'d implemented it, I\'d run a test to make sure that my algorithms should never collide. If the collision rate was high, then I\'d play with the parameters (amount of nouns used, amount of verbs used, length of random number, total number of words, different kinds of casings etc.)</p>\n'}, {'answer_id': 34757, 'author': 'Joseph Kingry', 'author_id': 3046, 'author_profile': 'https://Stackoverflow.com/users/3046', 'pm_score': 0, 'selected': False, 'text': "<p>If by user friendly, you mean that a user could type the answer in then I think you would want to look in a different direction. I've seen and done implementations for initial random passwords that pick random words and numbers as an easier and less error prone string. </p>\n\n<p>If though you're looking for a way to encode a random code in the URL string which is an issue I've dealt with for awhile then I what I have done is use 64-bit encoded GUIDs.</p>\n"}, {'answer_id': 34936, 'author': 'JasonS', 'author_id': 1865, 'author_profile': 'https://Stackoverflow.com/users/1865', 'pm_score': 0, 'selected': False, 'text': '<p>You could load your list of words as chakrit suggested into a data table or xml file with a unique sequential key.  When getting your random word, use a random number generator to determine what words to fetch by their key.  If you concatenate 2 of them, I don\'t think you need to include the numbers in the string unless "true randomness" is part of the goal.</p>\n'}, {'answer_id': 62920385, 'author': 'Peter O.', 'author_id': 815724, 'author_profile': 'https://Stackoverflow.com/users/815724', 'pm_score': 2, 'selected': False, 'text': '<p>At the time of this writing, this question\'s title is:</p>\n<blockquote>\n<p>How can I generate a unique, small, random, and user-friendly key?</p>\n</blockquote>\n<p>To that, I should note that it\'s not possible in general to create a <em>random</em> value that\'s also <em>unique</em>, at least if each random value is generated independently of any other.  In addition, there are many things you should ask yourself if you want to generate unique identifiers (which come from my <a href="https://peteroupc.github.io/random.html#Unique_Random_Identifiers" rel="nofollow noreferrer">section on unique random identifiers</a>):</p>\n<ol>\n<li>Can the application easily check identifiers for uniqueness within the desired scope and range (e.g., check whether a file or database record with that identifier already exists)?</li>\n<li>Can the application tolerate the risk of generating the same identifier for different resources?</li>\n<li>Do identifiers have to be hard to guess, be simply &quot;random-looking&quot;, or be neither?</li>\n<li>Do identifiers have to be typed in or otherwise relayed by end users?</li>\n<li>Is the resource an identifier identifies available to anyone who knows that identifier (even without being logged in or authorized in some way)?</li>\n<li>Do identifiers have to be memorable?</li>\n</ol>\n<p>In your case, you have several conflicting goals:  You want identifiers that are—</p>\n<ul>\n<li>unique,</li>\n<li>easy to type by end users (including small), and</li>\n<li>hard to guess (including random).</li>\n</ul>\n<p>Important points you don\'t mention in the question include:</p>\n<ul>\n<li>How will the key be used?</li>\n<li>Are other users allowed to access the resource identified by the key, whenever they know the key?  If not, then additional access control or a longer key length will be necessary.</li>\n<li>Can your application tolerate the risk of duplicate keys?  If so, then the keys can be completely randomly generated (such as by a cryptographic RNG).  If not, then your goal will be harder to achieve, especially for keys intended for security purposes.</li>\n</ul>\n<p>Note that I don\'t go into the issue of formatting a unique value into a &quot;user-friendly key&quot;.  There are many ways to do so, and they all come down to mapping unique values one-to-one with &quot;user-friendly keys&quot; — if the input value was unique, the &quot;user-friendly key&quot; will likewise be unique.</p>\n'}]
['https://Stackoverflow.com/questions/34708', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1931/']