Warning: this post is almost a complete waste of time. Read at your own risk.

Years ago, my team was working on a feature that required random passwords to be generated, hundreds at a time. While discussing different techniques for doing this, one of the guys suggested just using a Guid, but only a substring of it. Brilliant! I've used that trick ever since then, every time I need to generate random passwords that need to be easily entered for 1-time use.

I'm working on one of these features right now for an app I'm building. It struck me, I wonder which portion of a .NET generated Guid is the most random. Now, I'm sure I could Bing this and find a logical answer, but that's no fun. So I wrote some code to report on the uniqueness of 8-character strings within a Guid (formatted without the dashes).

   1: int max = 1000000;
   2:  
   3: string[] passwords = new string[max];
   4: Dictionary<int, int> uniques = new Dictionary<int, int>();
   5:  
   6: for (int p = 0; p < 24; p++)
   7: {
   8:   for (int i = 0; i < max; i++)
   9:     {
  10:         passwords[i] = Guid.NewGuid().ToString("N").Substring(p, 8);
  11:     }
  12:  
  13:     uniques.Add(p, passwords.Distinct().Count());
  14: }

First Run:

  1. 999875
  2. 999883
  3. 999876
  4. 999865
  5. 999893
  6. 998150
  7. 998114
  8. 998107
  9. 998114
  10. 992633
  11. 992633
  12. 992441
  13. 992711
  14. 999597
  15. 999553
  16. 999531
  17. 999535
  18. 999882
  19. 999879
  20. 999882
  21. 999897
  22. 999883
  23. 999891
  24. 999901

Second Run:

  1. 999891
  2. 999857
  3. 999872
  4. 999877
  5. 999889
  6. 998143
  7. 998056
  8. 998153
  9. 998129
  10. 992572
  11. 992579
  12. 992524
  13. 992547
  14. 999531
  15. 999530
  16. 999548
  17. 999545
  18. 999910
  19. 999881
  20. 999887
  21. 999883
  22. 999866
  23. 999874
  24. 999875

I find it interesting that with both runs, starting positions of 9-12 both saw a significant and consistent drop in the unique counts. And positions 0-4 and 17-23 appear to be the most random in both sets. Position 17 looks quite attractive; I'll use that.