Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Guid.NewGuid never produce a Guid that doesn't contain a 4?

Tags:

.net

guid

One would think the distribution of bytes in guids were random, or at least very flat. What is the reason that Guid.NewGuid always makes guids that contain a 4? whose string representation contains a 4?

That is

Guid.NewGuid().ToString("N").Contains("4")

is always true.

Quick testing indicates that most bytes occur in about 85% of Guids, but 4 occurs in 100%. Maybe this doesn't matter, but I would love to know why.

[Edit]
I wasn't terribly clear, so edited to improve the clarity of my question.


Run this. Not exactly profound, but fun.

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
}

}

I need to learn how to format stuff properly here some day. Stackoverflow is grrr-eat.

like image 826
The Dag Avatar asked Mar 08 '12 14:03

The Dag


People also ask

What does GUID NewGuid () do?

Guid. NewGuid() creates an empty Guid object, initializes it by calling CoCreateGuid and returns the object.

Can a GUID never be duplicated?

While each generated GUID is not guaranteed to be unique, the total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is very small.

Can a GUID be empty?

A Guid. Empty has a value of 00000000-0000-0000-0000-000000000000. You can take advantage of an empty GUID to compare it with another GUID object to determine if it is non-zero.

How many numbers are in a GUID?

A GUID is a 128-bit value consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits. The following example GUID shows the groupings of hexadecimal digits in a GUID: 6B29FC40-CA47-1067-B31D-00DD010662DA.


1 Answers

GUIDs are not completely random, the spot where the 4 is indicates the "type" of GUID being generated.

See http://en.wikipedia.org/wiki/Globally_unique_identifier

like image 166
Martin Avatar answered Nov 15 '22 00:11

Martin