
{"id":4818,"date":"2022-12-17T01:18:00","date_gmt":"2022-12-17T06:18:00","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4818"},"modified":"2022-12-17T01:18:17","modified_gmt":"2022-12-17T06:18:17","slug":"c-random-number-generation","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4818","title":{"rendered":"C++ random number generation"},"content":{"rendered":"<p>This post compares random number generation in &#8220;the old days&#8221;, and in modern C++.<\/p>\n<p>This is how we used to do it:<\/p>\n<pre>#include &lt;cstdlib.h&gt;\r\nsrand((unsigned)time(NULL));            \/\/ seed the random number generator with current time\r\nint random_number = 200 + rand() % 100; \/\/ random number between 200 and 299<\/pre>\n<p>This is how it looks in modern C++:<\/p>\n<pre>#include &lt;random&gt;\r\nrandom_device rd;                    \/\/ a device that allegedly has access to external entropy, like mouse\r\ndefault_random_engine engine(rd());  \/\/ random number generator based on seed\r\nuniform_int_distribution d(200,299); \/\/ uniform distribution between 200 and 299, inclusive\r\nint random_number = d(engine);       \/\/ random nunmber between 200 and 299<\/pre>\n<p>Good stuff: it looks really fancy and extensible.<br \/>\nBad stuff:<br \/>\n&#8211; I wish the naming were better. In real life an engine is a kind of device, so the differene between &#8220;device&#8221; and &#8220;engine&#8221; is confusing.<br \/>\n&#8211; The code is longer (duh)<br \/>\n&#8211; Using two objects, the distribution and the &#8220;engine&#8221;, to generate a number is annoying.<\/p>\n<p>One could easily create a single object that combines the distribution and the engine, but why extra work?<\/p>\n<pre>template&lt;typename Engine, typename IntegerType&gt;\r\nclass uniform_int_generator {\r\n    Engine engine_;\r\n    uniform_int_distribution d_;\r\npublic:\r\n    uniform_int_generator( IntegerType min, IntegerType  max, Engine&amp;&amp; engine ) :\r\n        d_(min,max),\r\n        engine_(move(engine)) {\r\n    }\r\n\r\n    IntegerType operator()() {\r\n        return d_(engine_);\r\n    }\r\n};\r\n\r\nint main() {\r\n    random_device rd;\r\n    uniform_int_generator g(200,299, default_random_engine(rd()));\r\n    for (int i=0; i&lt;10; ++i) {\r\n        cout &lt;&lt; g() &lt;&lt; endl;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post compares random number generation in &#8220;the old days&#8221;, and in modern C++. This is how we used to do it: #include &lt;cstdlib.h&gt; srand((unsigned)time(NULL)); \/\/ seed the random number <a href=\"https:\/\/ikriv.com\/blog\/?p=4818\" 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":[4],"tags":[],"class_list":["entry","author-ikriv","post-4818","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4818","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=4818"}],"version-history":[{"count":4,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4818\/revisions"}],"predecessor-version":[{"id":4822,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4818\/revisions\/4822"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}