Skip to content

Instantly share code, notes, and snippets.

@mitchdenny
Created April 3, 2011 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchdenny/900302 to your computer and use it in GitHub Desktop.
Save mitchdenny/900302 to your computer and use it in GitHub Desktop.
The purpose of this class is to generate a random short URL.
using System;
public static class ShortUrlTokenGenerator
{
private const string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
private static Random generator = new Random();
public static string GenerateToken(int tokenLength)
{
var builder = new StringBuilder();
for (var tokenIndex = 0; tokenIndex < tokenLength; tokenIndex++)
{
var randomCharacterIndex = generator.Next(alphabet.Length);
builder.Append(alphabet[randomCharacterIndex]);
}
return builder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment