{"id":18707,"date":"2025-05-12T09:33:46","date_gmt":"2025-05-12T09:33:46","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=18707"},"modified":"2025-05-12T11:18:20","modified_gmt":"2025-05-12T11:18:20","slug":"modern-way-for-randomization-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=18707","title":{"rendered":"Random number generation (C++)"},"content":{"rendered":"<p>Let&#8217;s generate 10 integers with a uniform distribution between 1 and 6.<\/p>\n<hr>\n<h2>Historic approach<\/h2>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;cstdlib&gt;\r\n#include &lt;ctime&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n    \/\/ Set up generator with seed\r\n    srand(time(NULL));\r\n\r\n    for (int n = 0; n != 10; ++n)\r\n        std::cout &lt;&lt;  rand() % 6 + 1 &lt;&lt; &#039; &#039;;\r\n    std::cout &lt;&lt; &#039;\\n&#039;;\r\n}\r\n<\/pre>\n<p>Only uniform distribution provided. \ud83d\udc4e<\/p>\n<hr>\n<h2>Modern approach<\/h2>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n#include &lt;random&gt;\r\n\r\nint main()\r\n{\r\n    \/\/ Set up generator with seed\r\n    std::mt19937 gen(std::random_device{}());\r\n\r\n    \/\/ Set up the distribution you want\r\n    std::uniform_int_distribution&lt;&gt; dist(1, 6);\r\n\r\n    for (int n = 0; n != 10; ++n)\r\n        std::cout &lt;&lt; dist(gen) &lt;&lt; &#039; &#039;;\r\n    std::cout &lt;&lt; &#039;\\n&#039;;\r\n}\r\n<\/pre>\n<p>Multiple distributions provided. \ud83d\udc4d<\/p>\n<hr>\n<p>Performance: <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=18216\">FlameGraph (Docker)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s generate 10 integers with a uniform distribution between 1 and 6. Historic approach Only uniform distribution provided. \ud83d\udc4e Modern<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18707"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18707"}],"version-history":[{"count":25,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18707\/revisions"}],"predecessor-version":[{"id":18735,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18707\/revisions\/18735"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18707"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}