{"id":4079,"date":"2016-03-25T20:35:04","date_gmt":"2016-03-25T20:35:04","guid":{"rendered":"https:\/\/really.zonky.org\/?p=4079"},"modified":"2016-03-25T20:35:04","modified_gmt":"2016-03-25T20:35:04","slug":"randomising-stuff-at-the-command-line","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=4079","title":{"rendered":"Randomising Stuff At The Command-Line"},"content":{"rendered":"<p>Recently I have been seeing quite a lot of usage of <a href=\"https:\/\/www.random.org\/\">random.org<\/a>\u00a0(to pick out winners of various kinds of competitions; and no I&#8217;m not a winner). The documentation on that site are reasonable with regard to pseudo-random number generators but are not quite correct with regard to the source of random numbers under Linux. And for non-cryptographic uses, the following is fine.<\/p>\n<p>The use of random.org momentarily made me wonder how I would do the equivalent at the Unix (or Linux) command-line, and having used the command before, the\u00a0<em>shuf\u00a0<\/em>command came to mind. To be honest shuffling is not what I think of randomisation given how bad I am at shuffling cards, but despite the name,\u00a0<em>shuf<\/em> does pretty well at randomising things :-<\/p>\n<pre>\u00bb <strong>seq 1 10 | shuf<\/strong>\r\n4\r\n5\r\n8\r\n7\r\n2\r\n1\r\n10\r\n9\r\n6\r\n3\r\n<\/pre>\n<p>The <em>seq<\/em> command generates a sequence from 1-10 as given. It turns out that <em>shuf<\/em> can do it itself :-<\/p>\n<pre>\u00bb <strong>shuf -i 1-10<\/strong>\r\n7\r\n3\r\n5\r\n6\r\n9\r\n10\r\n8\r\n1\r\n4\r\n2\r\n<\/pre>\n<p>The most common (relatively) use I have for <em>shuf<\/em> is to pick out a random line or two from a file. By using the <em>-n<\/em> option, <em>shuf<\/em> can do this. The following example makes use of an example file which contains a small number of first names :-<\/p>\n<pre>\u00bb <strong>shuf -n 1 first-names<\/strong> \r\nJulian\r\n\u00bb <strong>shuf -n 1 first-names<\/strong>\r\nIan\r\n\u00bb <strong>shuf -n 1 first-names<\/strong>\r\nCraig\r\n<\/pre>\n<p>If you have just a small selection to make, you can provide the list on the command line with the <em>-em<\/em> option :-<\/p>\n<pre>\u00bb <strong>shuf -n 1 -e Male Female<\/strong>\r\nFemale\r\n<\/pre>\n<p>And that is pretty much all there is to it &#8211; a simple tool that does just one thing well.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3904\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?resize=695%2C463&#038;ssl=1\" alt=\"damascus-unix-prompt\" width=\"695\" height=\"463\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?w=792&amp;ssl=1 792w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I have been seeing quite a lot of usage of random.org\u00a0(to pick out winners of various kinds of competitions; and no I&#8217;m not a winner). The documentation on that site are reasonable with regard to pseudo-random number generators but are not quite correct with regard to the source of random numbers under Linux. And <a href='https:\/\/really.zonky.org\/?p=4079' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[4,209],"tags":[1398,1397],"class_list":["post-4079","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it","tag-random","tag-shuf","category-4-id","category-209-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-13N","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/4079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4079"}],"version-history":[{"count":2,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/4079\/revisions"}],"predecessor-version":[{"id":4081,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/4079\/revisions\/4081"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}