title: Generate a random code that can be validated later
body: <p>I need to generate a verification code and send it to a customer, so that the customer can then provide the code later and I validate it. I want to be able to do this without storing the verification codes in a database, i.e., the codes should be self-containted.</p>
  
  <p>How can I generate the code and prevent the customer from just guessing what a valid code is? Obviously, I can't completely rule out a lucky guess, but I want the probability of such an event to be reasonably small, while keeping the code length small. A code may contain digits and letters.</p>
  
  <p>For example, a very basic verification code would be like this: randomly generate the number <code>1122</code>. Now, compute the verification code <code>11226</code> ( <code>1122</code> and append its checksum <code>6=1+1+2+2</code>). So, if the customer sends back <code>11226</code> back I would be able to validate it, but of course this is too easy to tamper with. Is there a method to generate such codes?</p>
  
  <p><strong>EDIT:</strong> The problem is that the system which sends the verification code and the system where the verification code is validated are separated, no information is shared between these two systems. There is no way that even the randomly generated number (<code>1122</code> in the example) is known to the validation system. Let me give you another example: in my country, there is a thing called "Orange Wednesday". You can get 2 movie tickets for the price of one if you have a code received by SMS (you send a SMS and receive a code by SMS if you are Orange customer). However, this code is not tied to anything (phone number or similar): I can give the code to someone else and it is still valid. I would need this kind of verification code.</p>