I am creating a random string, 4 characters long, as follows:
Int16 length = 4;
Random random = new Random();
String characters = "abcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder alias = new StringBuilder();
while (length-- > 0)
alias.Append(characters[(int)(random.NextDouble() *
characters.Length)]);
I suppose this is a good option but any suggestion is welcome.
Some minor cleanup:
Int16 length = 4;
Random random = new Random();
String characters = "abcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder alias = new StringBuilder(length);
while (length-- > 0)
alias.Append(characters[random.Next(characters.Length)]);
I like to initialize StringBuilders explicitly if I know the exact length
ahead of time, even if that length is the same or smaller than the default
initial capacity. And of course, the Random class already has a perfectly
fine way to get an integer in the range [0, upper-bound).
You could also make the "characters" variable a constant instead of
initializing it each time through the code.
I am inserting the random string into an SQL table. I need to be sure
that is unique ...
... if it isn't then I should recreate a new one.
What is the best way to do this?
To do what? Make sure the string is unique? That depends on what sources
the string could have, and over what period of time you need for the
instances to be unique. But, an obvious possibility is to simply inspect
the table before inserting it to see if it already exists. Or (I'm no SQL
expert, so bear with me if this makes no sense) use the string as the (a?)
key for the table, and simply check for an error trying to insert it (my
understanding is that SQL has an idea of a column in a table that has to
have a unique value for each record...I'm calling that column the "key").
If the string only needs to be unique for a given execution of your
program, you could maintain a dictionary (Hashtable, Dictionary<TKey,
TValue>, etc.) and check the dictionary for the presence of the key before
using it in your table. For that matter, if the table exists outside of a
given execution of your program (not uncommon for databases

), you
could initialize the dictionary when you start using the table, using the
current records in the table, before you start trying to add things to it..
Note that your 4-character key has "only" about a million and a half
possibilities. You may find the Guid class more useful, if you happen to
be dealing with really large numbers of data records. For that matter,
even a standard int (even if converted to a string) would give you about 4
billion possibilities instead.
Finally, note that it might make more sense just to assign these keys in
sequence, rather than picking them randomly. Unless you have some
specific need to obfuscate the selection pattern for the keys, the code
would be simpler and could deal with collisions in a much more efficient
way (i.e. just pick the next available one, rather than repeatedly trying
a random one until you find a non-duplicate).
Pete