{"id":13820,"date":"2022-10-05T12:35:48","date_gmt":"2022-10-05T07:05:48","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/"},"modified":"2022-10-05T12:35:48","modified_gmt":"2022-10-05T07:05:48","slug":"solved-weird-issue-in-console-app","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/","title":{"rendered":"[Solved] Weird issue in Console app"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-9626390\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"9626390\" data-parentid=\"9621384\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Ehem. I tried running the code at my HTPC, a different computer from the one I coded this on, and now I cannot reproduce the problem. That is, I do observe the burst leading to just a step, but that is due to a logical error in my code (when the report interval is reached it sets burst to -1). It&#8217;s hard to believe I did <em>not<\/em> set my breakpoint, stepped through, and saw the variable get destroyed, because I know how weird that would be and tried several times to be sure I saw what I thought I saw. But it&#8217;s also hard to believe I had stumbled upon such a weird and deep bug in the framework\/clr, especially considering that my code had a bug that causes the thing that got me attaching the debugger in the first place..<\/p>\n<p>In any case, I&#8217;ll mark it as closed. And post the revised code here if anyone wants to play with it. I&#8217;ve fixed the bug and made the output a bit more compact so it works better on less generous screens than the 22&#8243; full-HD_one I did this on. It now uses 8 columns regardless of the console width, on the probably safe assumption that most people use standard 80-char width, into which 8 columns now fit.<\/p>\n<p>If anyone would care run this and post their findings (just press P to quickly get stable frequencies, the step\/burst thing is for silly stuff like seeing what the distribution looks like after fewer generations). On my HTPC, I get this result:<\/p>\n<pre>\n0x00 - 0x3f  0.34%\n0x40 - 0x4f  0.73%\n0x50 - 0x7f  0.34%\n0x80 - 0xbf  0.44%\n0xc0 - 0xff  0.34%\n<\/pre>\n<p>This means: Bytes 0x00 to 0x3f each made up 0.34% of all the bytes in all the guids generated (509,194 in this particular case, but I get this result every time with more than 100,000 guids or so). There are 3 very distinct groups, and maybe if I now go and read about Guids on wikipedia I will understand why that is. But it wouldn&#8217;t be as much fun to do this if my &#8220;discovery&#8221; was something I knew before I began. \ud83d\ude42<\/p>\n<pre><code>using System;\nusing System.Diagnostics;\nusing System.Threading;\n\nnamespace ConsoleApplication1\n{\n    class Program\n    {\n        static bool running, exit;\n        static int burst;\n        static long guidCount = 0;\n        static long[] counts = new long[256];\n        static DateTime nextReport = DateTime.MinValue;\n        static readonly TimeSpan reportInterval = TimeSpan.FromSeconds(1);\n\n\n        static void Main(string[] args)\n        {\n            Console.WindowHeight = (int)(0.8 * Console.LargestWindowHeight);\n\n            WriteLine(ConsoleColor.White, \"X - Exit | P - Run\/Pause | S - Step (hold for Slow) | B - Burst\");\n            WriteLine(\"Press P, S or B to make something happen.\", reportInterval);\n\n            Guid guid;\n            byte[] bytes;\n\n            var cursorPos = new CursorLocation();\n\n            while (!exit)\n            {\n                if (Console.KeyAvailable)\n                {\n                    ProcessKey(Console.ReadKey(true));\n                }\n\n                if (running || burst &gt; 0)\n                {\n                    guid = Guid.NewGuid();\n                    bytes = guid.ToByteArray();\n                    ++guidCount;\n\n                    for (int i = 0; i &lt; 16; i++)\n                    {\n                        var b = bytes[i];\n                        ++counts[b];\n                    }\n\n                    if (burst &gt; 0) --burst;\n\n                    if (burst == 0 &amp;&amp; DateTime.Now &gt; nextReport)\n                    {\n                        cursorPos.MoveCursor();\n                        ReportFrequencies();\n                    }\n                }\n                else\n                    Thread.Sleep(20);\n            }\n        }\n\n\n        static void ProcessKey(ConsoleKeyInfo keyInfo)\n        {\n            switch (keyInfo.Key)\n            {\n                case ConsoleKey.P:\n                    running = !running;\n                    break;\n\n                case ConsoleKey.B:\n                    burst = 100;\n                    break;\n\n                case ConsoleKey.S:\n                    burst = 1;\n                    break;\n\n                case ConsoleKey.X:\n                    exit = true;\n                    break;\n            }\n        }\n\n\n        static void ReportFrequencies()\n        {\n            Write(\"\\r\\n{0} GUIDs generated. Frequencies (%):\\r\\n\\r\\n\", guidCount);\n\n            const int itemWidth = 11;\n            const int colCount = 8; \/\/ Console.WindowWidth \/ (itemWidth + 2);\n\n            for (int i = 0; i &lt; 256; i++)\n            {\n                var f = (double)counts[i] \/ (16 * guidCount);\n                var c = GetFrequencyColor(f);\n                Write(c, RightAdjust(3, \"{0:x}\", i));\n                Write(c, \" {0:0.00}\".PadRight(itemWidth), f*100);\n                if ((i + 1) % colCount == 0) Write(\"\\r\\n\");\n            }\n\n            nextReport = DateTime.Now + reportInterval;\n        }\n\n\n        static ConsoleColor GetFrequencyColor(double f)\n        {\n            if (f &lt; 0.003) return ConsoleColor.DarkRed;\n            if (f &lt; 0.004) return ConsoleColor.Green;\n            if (f &lt; 0.005) return ConsoleColor.Yellow;\n            return ConsoleColor.White;\n        }\n\n\n        static string RightAdjust(int w, string s, params object[] args)\n        {\n            if (args.Length &gt; 0)\n                s = string.Format(s, args);\n            return s.PadLeft(w);\n        }\n\n        #region From my library, so I need not include that here...\n        class CursorLocation\n        {\n            public int X, Y;\n            public CursorLocation()\n            {\n                X = Console.CursorLeft;\n                Y = Console.CursorTop;\n            }\n\n            public void MoveCursor()\n            {\n                Console.CursorLeft = X;\n                Console.CursorTop = Y;\n            }\n        }\n\n\n        static public void Write(string s, params object[] args)\n        {\n            if (args.Length &gt; 0) s = string.Format(s, args);\n            Console.Write(s);\n        }\n\n\n        static public void Write(ConsoleColor c, string s, params object[] args)\n        {\n            var old = Console.ForegroundColor;\n            Console.ForegroundColor = c;\n            Write(s, args);\n            Console.ForegroundColor = old;\n        }\n\n\n        static public void WriteNewline(int count = 1)\n        {\n            while (count-- &gt; 0) Console.WriteLine();\n        }\n\n\n        static public void WriteLine(string s, params object[] args)\n        {\n            Write(s, args);\n            Console.Write(Environment.NewLine);\n        }\n\n\n        static public void WriteLine(ConsoleColor c, string s, params object[] args)\n        {\n            Write(c, s, args);\n            Console.Write(Environment.NewLine);\n        }\n        #endregion\n    }\n}\n<\/code><\/pre>\n<p>Post your results, ladies and gentlemen. \ud83d\ude42<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Weird issue in Console app <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Ehem. I tried running the code at my HTPC, a different computer from the one I coded this on, and now I cannot reproduce the problem. That is, I do observe the burst leading to just a step, but that is due to a logical error in my code (when the report interval is &#8230; <a title=\"[Solved] Weird issue in Console app\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\" aria-label=\"More on [Solved] Weird issue in Console app\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,541],"class_list":["post-13820","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-console"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Weird issue in Console app - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Weird issue in Console app - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Ehem. I tried running the code at my HTPC, a different computer from the one I coded this on, and now I cannot reproduce the problem. That is, I do observe the burst leading to just a step, but that is due to a logical error in my code (when the report interval is ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-05T07:05:48+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Weird issue in Console app\",\"datePublished\":\"2022-10-05T07:05:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\"},\"wordCount\":364,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"console\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\",\"name\":\"[Solved] Weird issue in Console app - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-05T07:05:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Weird issue in Console app\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Weird issue in Console app - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Weird issue in Console app - JassWeb","og_description":"[ad_1] Ehem. I tried running the code at my HTPC, a different computer from the one I coded this on, and now I cannot reproduce the problem. That is, I do observe the burst leading to just a step, but that is due to a logical error in my code (when the report interval is ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/","og_site_name":"JassWeb","article_published_time":"2022-10-05T07:05:48+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Weird issue in Console app","datePublished":"2022-10-05T07:05:48+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/"},"wordCount":364,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","console"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/","url":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/","name":"[Solved] Weird issue in Console app - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-05T07:05:48+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-weird-issue-in-console-app\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Weird issue in Console app"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=13820"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13820\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=13820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=13820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=13820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}