{"id":1318,"date":"2015-12-07T23:33:38","date_gmt":"2015-12-08T07:33:38","guid":{"rendered":"http:\/\/www.elbeno.com\/blog\/?p=1318"},"modified":"2015-12-07T23:33:38","modified_gmt":"2015-12-08T07:33:38","slug":"the-c-random-lame-list","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/blog\/?p=1318","title":{"rendered":"The C++ <code>&lt;random&gt;<\/code> Lame List"},"content":{"rendered":"<p>Network programmers of a certain age may remember the <a href=\"https:\/\/www.google.com\/search?client=ubuntu&#038;channel=fs&#038;q=windows+sockets+lame+list\">Windows Sockets Lame List<\/a>.<\/p>\n<p>I previously wrote a short <a href=\"https:\/\/www.elbeno.com\/blog\/?p=1081\">&#8220;don&#8217;t-do-that-do-this&#8221; guide for modern C++ randomness<\/a>, and I was recently reading another Reddit exchange featuring <a href=\"https:\/\/www.reddit.com\/user\/STL\/\">STL<\/a>, author of many parts of Microsoft&#8217;s STL implementation, when it struck me that use of C++ <code>&lt;random&gt;<\/code> needs its own lame list to discourage using the old and busted C parts and encourage the using the new C++ hotness. So here, in no particular order, and with apologies to Keith Moore (wherever he may be) is an incomplete lame list for use of <code>&lt;random&gt;<\/code>.<\/p>\n<ol>\n<li>Calling <code>rand()<\/code> or <code>srand()<\/code>. <strong>Lame.<\/strong><\/li>\n<li>Using <code>time(NULL)<\/code> to seed an RNG. <strong>Inexcusably lame.<\/strong><\/li>\n<li>Claiming, &#8220;But <code>rand()<\/code> is good enough for simple uses!&#8221; <strong>Dog lame.<\/strong><\/li>\n<li>Using <code>random_shuffle()<\/code> to permute a container. <strong>Mired in a sweaty mass of lameness.<\/strong><\/li>\n<li>Using <code>default_random_engine<\/code>. <strong>Nauseatingly lame.<\/strong><\/li>\n<li>Using <code>%<\/code> to get a random value in a range. <strong>Lame. Lame. Lame. Lame. Lame.<\/strong><\/li>\n<li>Not using <code>random_device<\/code> to seed an RNG. <strong>Violently lame<\/strong>.<\/li>\n<li>Assuming that <code>random_device<\/code> is going to do the right thing on your platform. <strong>Uncontrollably lame<\/strong>.<\/li>\n<li>Failing to handle a possible exception from the construction or use of <code>random_device<\/code>. <strong>Totally lame.<\/strong><\/li>\n<li>Using anything in the standard but <code>mt19937<\/code> or <code>mt19937_64<\/code> as a generator. <strong>Intensely lame.<\/strong><\/li>\n<li>Putting <code>mt19937<\/code> on the stack. <strong>In all my years of observing lameness, I have seldom seen something this lame.<\/strong><\/li>\n<li>Seeding <code>mt19937<\/code> with only one 32-bit word rather than its full <code>state_size<\/code>. <strong>Pushing the lameness envelope.<\/strong><\/li>\n<li>Forgetting that <code>uniform_int_distribution<\/code> works on a closed interval. <strong>Thrashing in a sea of lameness.<\/strong><\/li>\n<li>Passing <code>random_device<\/code> or a generator to <code>generate_n()<\/code> by value, forgetting to wrap it with <code>ref()<\/code>. <strong>Glaringly lame.<\/strong><\/li>\n<li>Failing to use <code>seed_seq<\/code> to initialize a generator&#8217;s state properly. <strong>Indescribably lame.<\/strong><\/li>\n<li>Not considering thread safety when using a generator. <strong>Floundering in an endless desert of lameness.<\/strong><\/li>\n<li>Using a global generator without making it <code>thread_local<\/code>. <strong>Suffocating in self lameness.<\/strong><\/li>\n<li>Using <code>RAND_MAX<\/code> instead of <code>mt19937::max()<\/code>. <strong>Perilously teetering on the edge of a vast chasm of lameness.<\/strong><\/li>\n<\/ol>\n<p>This list will undoubtedly grow as I continue to write lame code&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Network programmers of a certain age may remember the Windows Sockets Lame List. I previously wrote a short &#8220;don&#8217;t-do-that-do-this&#8221; guide for modern C++ randomness, and I was recently reading another Reddit exchange featuring STL, author of many parts of Microsoft&#8217;s STL implementation, when it struck me that use of C++&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-1318","post","type-post","status-publish","format-standard","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1318"}],"version-history":[{"count":6,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1318\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1318\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}