最近我决定调查用
Guid.NewGuid
method生成的全球唯一标识符的随机程度(这也是这个问题的范围).我记录了自己在
pseudorandom numbers,pseudorandomness,我很眩目,发现甚至有
random numbers generated by radioactive decay.无论如何,我会让你发现自己更多的细节关于这样有趣的讲座.
为了继续我的问题,GUID的另一件重要的事情是:
V1 GUIDs which contain a MAC address and time can be identified by the
digit “1” in the first position of the third group of digits,for
example {2F1E4FC0-81FD-11DA-9156-00036A0F876A}.V4 GUIDs use the later algorithm,which is a pseudo-random number.
These have a “4” in the same position,for example
{38A52BE4-9352-453E-AF97-5C3B448652F0}.
将它放在句子中,Guid将始终将数字4(或1,但不在我们的范围内)作为其组成部分之一.
对于我的GUID随机测试,我决定计算一些越来越大的GUID集合中的位数,并将其与数字发生的统计概率进行比较,期望发生.或者至少我希望我做到了(请原谅任何统计公式的错误,我只是尝试我最好的猜测来计算价值).我使用了下面列出的一个小型C#控制台应用程序.
class Program { static char[] digitsChar = "0123456789".tocharArray(); static decimal expectedOccurrence = (10M * 100 / 16) * 31 / 32 + (100M / 32); static void Main(string[] args) { for (int i = 1; i <= 10; i++) { CalculateOccurrence(i); } } private static void CalculateOccurrence(int counter) { decimal sum = 0; var sBuilder = new StringBuilder(); int localCounter = counter * 20000; for (int i = 0; i < localCounter; i++) { sBuilder.Append(Guid.NewGuid()); } sum = (sBuilder.ToString()).tocharArray() .Count(j => digitsChar.Contains(j)); decimal actualLocalOccurrence = sum * 100 / (localCounter * 32); Console.WriteLine(String.Format("{0}\t{1}",expectedOccurrence,Math.Round(actualLocalOccurrence,3) )); } }
以上程序的输出为:
63.671875 63.273 63.671875 63.300 63.671875 63.331 63.671875 63.242 63.671875 63.292 63.671875 63.269 63.671875 63.292 63.671875 63.266 63.671875 63.254 63.671875 63.279
所以即使理论上的发生预计为63.671875%,实际值约在63.2%左右.
这个差异如何解释?我的公式有错吗? Guid算法中有其他“晦涩”的规则吗?