{"id":97716,"date":"2020-10-26T15:00:00","date_gmt":"2020-10-26T12:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-generate-better-random-numbers-at-the-bash-command-line-cloudsavvy-it\/"},"modified":"2020-10-26T15:00:00","modified_gmt":"2020-10-26T12:00:00","slug":"how-to-generate-better-random-numbers-at-the-bash-command-line-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-generate-better-random-numbers-at-the-bash-command-line-cloudsavvy-it\/","title":{"rendered":"#How to Generate Better Random Numbers At The Bash Command Line \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e0fbd90e03\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e0fbd90e03\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-generate-better-random-numbers-at-the-bash-command-line-cloudsavvy-it\/#Random_Numbers_At_The_Terminal\" >Random Numbers At The Terminal<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Generate Better Random Numbers At The Bash Command Line \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure id=\"attachment_7610\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7610 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/9620eff434555414530544c9c0b58f6f\/p\/uploads\/2020\/10\/1aac3b56.png\" alt=\"\" width=\"700\" height=\"300\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/red-casino-dices-chips-on-silver-317612813\" data-credittext=\"Shutterstock\/David Orcea\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/red-casino-dices-chips-on-silver-317612813\">Shutterstock\/David Orcea<\/a><\/span><\/figcaption><\/figure>\n<p>Generating random numbers in bash seems simple to do using the <code>$RANDOM<\/code> variable, but is the variable truly that random? Find out what may be preventing you generating high quality random numbers, and more!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Random_Numbers_At_The_Terminal\"><\/span>Random Numbers At The Terminal<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It seems simple to generate a random number in Bash:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7573\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/1b809a493d49d2733a3c1ac245102fd6\/p\/uploads\/2020\/10\/2cbcd7fa.png\" alt=\"Generating a random number in Bash\" width=\"163\" height=\"73\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>But is the number truly random?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7574\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/0e7778aaea0f54e125e5adb0a7019292\/p\/uploads\/2020\/10\/3e314dba.png\" alt=\"Is a random number in Bash truly random?\" width=\"161\" height=\"268\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Not really, as we can see. The random number generator in Bash depends on a seed \u2013 a value passed to the random number generation function \u2013 which, provided the seed is the same, will always generate the same sequence of random numbers as the example output above shows.<\/p>\n<p>We can initialize the random number generator with a seed value by setting the <code>RANDOM<\/code>\u00a0variable to the desired seed value. So perhaps we can provide a random number as seed to the random generator?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7575\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/3ae9e9845178e83365da0cfb89ab82e8\/p\/uploads\/2020\/10\/26ec3e78.png\" alt=\"Assigning a random number as seed to the random generator in Bash\" width=\"201\" height=\"280\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>It seems to work for a little, each time we want to generate a random number we pre-seed the random number generator with a randomly generated number. But all we did was trick ourselves for a little while; we just created one extra layer of depth, but the outcome is about the same; numbers are not random and can be influenced by a fixed seed provided earlier.<\/p>\n<p>This problem is called a problem of \u2018random entropy\u2019 generation. The more entropy we can generate, the better our random numbers will be. This particular problem is not limited to Bash only, it exists in all basic computer systems which try to generate random numbers. Random is thus never really random. Some other random systems use for example mouse movements and keyboard strokes and other semi-random input in combination to increase the complexity of the random entropy pool.<\/p>\n<p>So, how can we generate a random number \u2018good enough\u2019 to be named truly random?<\/p>\n<p>For this, we would, as a source and seed, require something which is truly, or near to truly, random. We could think about using the date of today, but that isn\u2019t very random on second thoughts. How about the seconds since 00:00:00 UTC on 1 January 1970 (<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly called \u2018epoch\u2019 in Linux circles)? Maybe, but all one needs is a log file somewhere and the epoch can be reconstructed.<\/p>\n<p>A better solution is using the least significant digits of the nanosecond precision timer:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7578\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/1752943f4bd2c86da8eba1e870ebc8cf\/p\/uploads\/2020\/10\/60393885.png\" alt=\"A better random number seed generator\" width=\"319\" height=\"243\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In principle, even this is not perfect. It may fall under the header of \u2018better random number generation\u2019 as per the title of this article, but the entropy is not perfect by definition. Let\u2019s look at this a little closer.<\/p>\n<p>In the example, we take bytes 4 to 9 or 6 numbers from the epoch time, as expressed by <code>date +%N<\/code> and output as a result of the subshell initiated by\u00a0<code>$(...)<\/code>. This means our minimum seed is 0 and our maximum seed is 999999. This is only a range of 1 million numbers.<\/p>\n<p>In principle, this system could still be \u2018hacked\u2019: one could simply cycle through all those 1 million numbers and grab the random number sequences generated from that. It surely would be a very poor solution for an encryption key generation, for example!<\/p>\n<p>If we select less numbers, the risk for this gets larger. If we select more, the risk becomes smaller, but the \u2018random seed\u2019 becomes less random too. This can be exemplified by including the seconds since epoch:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7577\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/d593e13a33358c1ab354d8905677e6ad\/p\/uploads\/2020\/10\/2f135c5b.png\" alt=\"The issue with random entropy generation using seconds\" width=\"260\" height=\"287\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can see the seconds ticking over! Note the initial 6 &gt; 7 &gt; 8 etc.<\/p>\n<p>For standard random number generation purposes \u2013 for example in test software which varies it\u2019s testing <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach based on a random seed provided to it, the nanosecond based solution is sufficient\/enough. For other solutions which may need even better quality random numbers, an external hardware based solution may be required.<\/p>\n<p>True random number generation is a not a straightforward matter. There are hardware based solutions which may come close to, or achieve, true random entropy and\/or random number generation. Especially devices which are not only hardware based may be the key to generate that perfect random number.\n<\/p><\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/7572\/how-to-generate-better-random-numbers-at-the-bash-command-line\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Generate Better Random Numbers At The Bash Command Line \u2013 CloudSavvy IT&#8221; Shutterstock\/David Orcea Generating random numbers in bash seems simple to do using the $RANDOM variable, but is the variable truly that random? Find out what may be preventing you generating high quality random numbers, and more! Random Numbers At The Terminal&#8230;<\/p>\n","protected":false},"author":1,"featured_media":97717,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/1aac3b56.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-97716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/97716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=97716"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/97716\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/97717"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=97716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=97716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=97716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}