为什么Guid.NewGuid永远不会生成不包含4的Guid?

5
有人可能认为GUID中字节的分布是随机的,或者至少非常平坦。为什么Guid.NewGuid总是生成包含数字 4 的 GUID 呢?即 Guid.NewGuid().ToString("N").Contains("4") 总是为 true。
快速测试表明,大约 85% 的 GUID 中出现了大多数字节,但数字 4 在所有 GUID 中都出现了。也许这并不重要,但我很想知道为什么会这样。
[编辑] 我的问题表述不太清楚,因此进行了编辑以提高问题的清晰度。
运行这个代码。虽然不是非常深奥,但很有趣。 using System; using System.Diagnostics;
namespace ConsoleApplication1 { class Program { static bool paused, exit;
    static void Main(string[] args)
    {
        Console.WindowHeight = (int)(0.8*Console.LargestWindowHeight);

        var reportInterval = TimeSpan.FromSeconds(0.15);
        WriteLine(ConsoleColor.White, "X key to exit.");

        Guid guid;
        byte[] bytes;
        long guidCount = 0;
        var counts = new long[256];
        var watch = Stopwatch.StartNew();
        var cursorPos = new CursorLocation();

        while (!exit)
        {
            if (!paused)
            {
                guid = Guid.NewGuid();
                bytes = guid.ToByteArray();
                ++guidCount;

                for (int i = 0; i < 16; i++)
                {
                    var b = bytes[i];
                    ++counts[b];
                }

                if (watch.Elapsed > reportInterval)
                {
                    cursorPos.MoveCursor();
                    DumpFrequencies(counts, guidCount);
                    watch.Restart();
                }
            }

            if (Console.KeyAvailable)
            {
                ProcessKey(Console.ReadKey());
            }
        }
    }


    static void ProcessKey(ConsoleKeyInfo keyInfo)
    {
        switch (keyInfo.Key)
        {
            case ConsoleKey.P:
                paused = !paused;
                break;
            case ConsoleKey.X:
                exit = true;
                break;
        }
    }


    static void DumpFrequencies(long[] byteCounts, long guidCount)
    {
        Write("\r\n{0} GUIDs generated. Frequencies:\r\n\r\n", guidCount);

        const int itemWidth = 9;
        int colCount = Console.WindowWidth / (itemWidth*2);

        for (int i = 0; i < 256; i++)
        {
            var f = (double)byteCounts[i] / (16 * guidCount);
            Write(RightAdjust(itemWidth, "{0:x}", i));
            Write(GetFrequencyColor(f), " {0:p}".PadRight(itemWidth), f);
            if ((i + 1) % colCount == 0) Write("\r\n");
        }
    }


    static ConsoleColor GetFrequencyColor(double f)
    {
        if (f < 0.003) return ConsoleColor.DarkRed;
        if (f < 0.004) return ConsoleColor.Green;
        if (f < 0.005) return ConsoleColor.Yellow;
        return ConsoleColor.White;
    }


    static string RightAdjust(int w, string s, params object[] args)
    {
        if (args.Length > 0)
            s = string.Format(s, args);
        return s.PadLeft(w);
    }

    #region From my library, so I need not include that here...
    class CursorLocation
    {
        public int X, Y;
        public CursorLocation()
        {
            X = Console.CursorLeft;
            Y = Console.CursorTop;
        }

        public void MoveCursor()
        {
            Console.CursorLeft = X;
            Console.CursorTop = Y;
        }
    }


    static public void Write(string s, params object[] args)
    {
        if (args.Length > 0) s = string.Format(s, args);
        Console.Write(s);
    }


    static public void Write(ConsoleColor c, string s, params object[] args)
    {
        var old = Console.ForegroundColor;
        Console.ForegroundColor = c;
        Write(s, args);
        Console.ForegroundColor = old;
    }


    static public void WriteNewline(int count = 1)
    {
        while (count-- > 0) Console.WriteLine();
    }


    static public void WriteLine(string s, params object[] args)
    {
        Write(s, args);
        Console.Write(Environment.NewLine);
    }


    static public void WriteLine(ConsoleColor c, string s, params object[] args)
    {
        Write(c, s, args);
        Console.Write(Environment.NewLine);
    }
    #endregion
}

我需要在某一天学习如何正确地格式化内容。Stackoverflow非常棒。


5
GUIDs不是随机的。 - Konrad Rudolph
@KonradRudolph,这就是为什么我对字节的 分布 强调如此之具体,因为它们要么是随机的,要么至少非常平坦。我知道它们不完全是随机的,但不知道原因。 - The Dag
2
停止浪费我们的 GUID! - U1199880
1个回答

8

1
太好了。我刚刚在制作支持搜索即时输入的树形视图控件时偶然发现了这个问题。为了测试,我使用Guid上的ToString("N")创建了巨大且准随机的树,以获取可以搜索的文本。该控件显示有多少匹配项,突出显示匹配节点,滚动第一个匹配项到视图中,并允许用户导航到下一个/上一个匹配项(带环绕)。它运行良好,但当我键入“4”时,在我的10万个节点树中看到了100,000个匹配项,这让我非常惊讶。 :) - The Dag

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接