
{"id":5213,"date":"2025-03-30T02:20:07","date_gmt":"2025-03-30T06:20:07","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5213"},"modified":"2025-04-09T20:58:59","modified_gmt":"2025-04-10T00:58:59","slug":"c-random-number-generation-mt19337-to-ranlux24","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5213","title":{"rendered":"C++ random number generation: mt19937 to ranlux24"},"content":{"rendered":"<p>It&#8217;s pretty old stuff, but still&#8230;<\/p>\n<p>Here&#8217;s how you typically generate a random number in C++:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n    std::random_device rd;\r\n    std::mt19937 gen(rd());\r\n    std::uniform_int_distribution&lt;int&gt; dis(1, 100);\r\n    auto random_number = dis(gen);\r\n<\/pre>\n<p>Let&#8217;s dissect it a little. On the one hand, &#8220;<code>random_device<\/code>&#8221; is a rather nebulous concept: it may or may not be truly random, and it may or may not be cryptographically secure. Truth be told, it&#8217;s completely implementation dependent. On the other hand, &#8220;<code>mt19937<\/code>&#8221; is an awfully specific random number generator implementation. These two creatures from two different worlds are usually used in tandem: the above is a boiler-plate example for generating random numbers in C++.<\/p>\n<p>Seasoned C++ programmers got used to a class named <code>mt19937<\/code>, but to the regular folks this resembles a Reddit user name rather than class name. If I saw a class named <code>fb24498<\/code> in a pull request, I would reject it, and so would most of my colleagues. But it is somehow OK for the C++ standard. As a user, do I <b>really<\/b> need to remember the magic number 19937 and what &#8220;mt&#8221; stands for? Isn&#8217;t it, like &#8220;multi-threaded&#8221; or something? Don&#8217;t I have other important things to care about? Of course, I can always use <code>ranlux24<\/code>, but it&#8217;s hardly better.<\/p>\n<p>Compare it to Python:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport random\r\nrandom_number = random.randint(1,100)\r\n<\/pre>\n<p>The seeding is done for you automatically, no questions asked. Yes, it can be argued that this is kind of vague, but so is <code>random_device<\/code>. C++ version does not add much clarity, but totally kills the ease of use.<\/p>\n<p>How about C# then?<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar rng = new Random();\r\nint num1 = rng.Next(1, 100);\r\n<\/pre>\n<p>Go?<\/p>\n<pre class=\"brush: go; title: ; notranslate\" title=\"\">\r\nrand.Seed(time.Now().UnixNano());\r\nnum := 1+rand.Intn(100);\r\n<\/pre>\n<p>Nobody but C++ seems to force users to remember magic numbers like 19937&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s pretty old stuff, but still&#8230; Here&#8217;s how you typically generate a random number in C++: std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution&lt;int&gt; dis(1, 100); auto random_number = dis(gen); Let&#8217;s dissect it <a href=\"https:\/\/ikriv.com\/blog\/?p=5213\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8,7,4,26],"tags":[],"class_list":["entry","author-ikriv","post-5213","post","type-post","status-publish","format-standard","category-cs","category-cpp","category-hack","category-python"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5213"}],"version-history":[{"count":6,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5213\/revisions"}],"predecessor-version":[{"id":5239,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5213\/revisions\/5239"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}