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:
- 999875
- 999883
- 999876
- 999865
- 999893
- 998150
- 998114
- 998107
- 998114
- 992633
- 992633
- 992441
- 992711
- 999597
- 999553
- 999531
- 999535
- 999882
- 999879
- 999882
- 999897
- 999883
- 999891
- 999901
Second Run:
- 999891
- 999857
- 999872
- 999877
- 999889
- 998143
- 998056
- 998153
- 998129
- 992572
- 992579
- 992524
- 992547
- 999531
- 999530
- 999548
- 999545
- 999910
- 999881
- 999887
- 999883
- 999866
- 999874
- 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.