{"id":13355,"date":"2021-12-27T09:41:24","date_gmt":"2021-12-27T09:41:24","guid":{"rendered":"https:\/\/linuxways.net\/?p=13355"},"modified":"2022-01-01T13:31:25","modified_gmt":"2022-01-01T13:31:25","slug":"generating-random-numbers-from-linux-terminal","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/generating-random-numbers-from-linux-terminal\/","title":{"rendered":"Generating Random Numbers From Linux Terminal"},"content":{"rendered":"<p>Generating random numbers is a fun kind of work to do. Sometimes, such tasks are needed for a purpose too like creating a password. In Linux, there are several ways to generate random numbers with some scripts.<\/p>\n<p>In this article, we are going to explain the different ways to generate random numbers through Linux terminals.<\/p>\n<h2>Use of $RANDOM variable<\/h2>\n<p>One way is to use the $RANDOM variable to generate random numbers within the range of 0 and 32767. It is a builtin shell variable to generate the random number. To create random numbers, run the command as shown below.<\/p>\n<pre>$ echo $RANDOM<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"523\" height=\"110\" class=\"wp-image-13356\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-521.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-521.png 523w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-521-300x63.png 300w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/p>\n<h2>Generate random number between the chosen range<\/h2>\n<p>With the small tweak on a command, you are able to generate random numbers between the chosen range. To generate a random number between the range 0 to 30, you have to run the command as shown below.<\/p>\n<pre>$ a=$(( $RANDOM % 31 ))<\/pre>\n<pre>$ echo $a<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"57\" class=\"wp-image-13357\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-522.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-522.png 528w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-522-300x32.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/p>\n<p>Another way to generate a random number between 0 to 100, you have to run the command as shown below.<\/p>\n<pre>$ echo $(( $RANDOM % 101 ))<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"531\" height=\"76\" class=\"wp-image-13358\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-523.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-523.png 531w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-523-300x43.png 300w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/p>\n<h2>Generate random number using the script<\/h2>\n<p>With the use of bash scripting, random numbers can also be easily generated. It is also a bit fun to write the script for such a purpose. Simple example of a script to generate a random number is shown below.<\/p>\n<pre>$ sudo vim randomnumber.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"374\" class=\"wp-image-13359\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-524.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-524.png 592w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-524-300x190.png 300w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<pre>#!\/bin\/bash\r\n\r\necho \"Enter the smallest number:\"\r\n\r\nread smallest\r\n\r\necho \"Enter the largest number:\"\r\n\r\nread largest\r\n\r\nif [[ $largest &lt; $smallest ]]; then\r\n\r\necho \"Your provided numbers is not valid\"\r\n\r\nexit 1\r\n\r\nfi\r\n\r\nchange=$(( $largest-$smallest ))\r\n\r\nif [[ $change == 1 ]]; then\r\n\r\necho \"Range between the numbers must be more than 1\"\r\n\r\nexit 1\r\n\r\nfi\r\n\r\nrandomnumber=$(( $RANDOM % $largest + 1))\r\n\r\necho \"The random number is: $randomnumber\"<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"359\" class=\"wp-image-13360\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-525.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-525.png 524w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-525-300x206.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/strong><\/p>\n<p>With such a script, you are able to generate random numbers between the range provided by you.<\/p>\n<h2>Use of shuf command<\/h2>\n<p>It is also one of the ways to generate random numbers between the ranges. Run a simple command on the Linux terminal for such a purpose. To generate the random number between the range 7 and 57, run the command as shown below.<\/p>\n<pre>$ shuf -i 7-57 -n1<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"93\" class=\"wp-image-13361\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-526.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-526.png 496w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-526-300x56.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/p>\n<h3>Use of tr command with urandom<\/h3>\n<p>To generate the random number with \u201cn\u201d number of digits, you can pull numbers from the \/dev\/urandom stream with the use of tr command. Suppose to generate a random number with 3 and 7 digits, you have to run the command as shown below.<\/p>\n<pre>$ tr -cd \"[:digit:]\" &lt; \/dev\/urandom | head -c 3<\/pre>\n<pre>$ tr -cd \"[:digit:]\" &lt; \/dev\/urandom | head -c 7<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"73\" class=\"wp-image-13362\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-527.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-527.png 627w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/12\/word-image-527-300x35.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, you have learnt to generate random numbers with a small command on the terminal or by creating the script. Also, you learnt to create random numbers between the range as provided. Thank you!<\/p>","protected":false},"excerpt":{"rendered":"<p>Generating random numbers is a fun kind of work to do. Sometimes, such tasks are needed for a purpose too like creating a password. In Linux, there are&hellip;<\/p>","protected":false},"author":1,"featured_media":13521,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[242,805],"class_list":["post-13355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-linux-terminal","tag-random-numbers"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/13355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=13355"}],"version-history":[{"count":1,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/13355\/revisions"}],"predecessor-version":[{"id":13363,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/13355\/revisions\/13363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/13521"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=13355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=13355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=13355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}