Instead of going through all that, why don't you just create a GUID and give them a substring of the generated GUID?
string password = Guid.NewGuid().ToString("N").Substring(5, 8);
No threading is needed.
EDIT ==============
The 1 vote on this answer is unjustified. My solution is
a) Faster than the what was posted in the original question, and
b) generates STRONGER random passwords because they may include alphabetic characters as well as numbers.
If you want proof of the randomness of using the
Guid.NewGuid
method, run this code in a WinForms app:
private void CreateGuids()
{
HashSet<string> hash = new HashSet<string>();
for (int i = 0; i < 1000; i++)
{
hash.Add(Guid.NewGuid().ToString("N").Substring(5,8));
}
MessageBox.Show(string.Format("{0} unique passwords generated.", hash.Count));
}
There is no denying that this is a better solution than building a password one character at a time, and calling
Random.Next
for each character.