{"id":5852,"date":"2024-06-17T03:00:00","date_gmt":"2024-06-17T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=5852"},"modified":"2024-06-18T11:02:09","modified_gmt":"2024-06-18T15:02:09","slug":"play-a-fun-math-game-with-linux-commands","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=5852","title":{"rendered":"Play a fun math game with Linux commands"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"5852\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>I like to play puzzle games as a way to take a break when I\u2019m working. One game that I like to play is borrowed from a UK quiz show called <em>Countdown<\/em> where players are given several random numbers, plus a random target number, and they need to use simple arithmetic to get within 10 of the target number, using the list of numbers at most one time. I find this is a fun math quiz to play on my own, and I\u2019ve written a short script that generates the numbers, to test myself if I can get the target number.<\/p>\n\n\n\n<p>The original game has the random numbers on a set of cards: the \u201cSmall\u201d numbers 1 to 10 are included twice and the \u201cLarge\u201d numbers 25, 50, 75, and 100 are listed just once. Players can ask for a combination of six numbers picked from \u201cSmall\u201d and \u201cLarge.\u201d The target number is a randomly generated number from 101 to 999, picked by a computer. I find the game is the most fun with two \u201cLarge\u201d and four \u201cSmall\u201d numbers; the game is much harder with other combinations, but I\u2019m just in it for fun.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"small-numbers\">Small numbers<\/h2>\n\n\n\n<p>Let\u2019s start by generating the \u201cSmall\u201d numbers. This list contains the numbers 1 to 10, twice. The <code>seq<\/code> command can generate a list of numbers in any range. If you specify two numbers as arguments, <code>seq<\/code> will start and end with those values. Just one number will start counting from 1 until it reaches that value. You can also add a third argument to include a \u201cstep\u201d or increment value. You can see the usage in these three examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ seq 3\n1\n2\n3\n$ seq 2 5\n2\n3\n4\n5\n$ seq 3 2 10\n3\n5\n7\n9<\/code><\/pre>\n\n\n\n<p>To list the \u201cSmall\u201d numbers, we just need to use <code>seq<\/code> twice:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ seq 10; seq 10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10<\/code><\/pre>\n\n\n\n<p>To pick just four numbers at random from this list, we first need to use the <code>shuf<\/code> command. This randomizes its input, displaying the lines in random order. After that, the <code>head<\/code> command can pick off just the first four values from the shuffled list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ (seq 10; seq 10) | shuf | head -4\n3\n6\n3\n8<\/code><\/pre>\n\n\n\n<p>Because <code>shuf<\/code> shuffles the lines from the input, we\u2019ll get a different set of four \u201cSmall\u201d number every time we run this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ (seq 10; seq 10) | shuf | head -4\n7\n3\n2\n4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"large-numbers\">Large numbers<\/h2>\n\n\n\n<p>We can use the same method to pick just two random values from the \u201cLarge\u201d numbers. Again, the <code>seq<\/code> command can generate the list for us, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ seq 25 25 100\n25\n50\n75\n100<\/code><\/pre>\n\n\n\n<p>And we can use <code>shuf<\/code> to randomize the list, and <code>head<\/code> to print just the first two numbers from the randomized list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ seq 25 25 100 | shuf | head -2\n75\n100<\/code><\/pre>\n\n\n\n<p>Every time we run this command, the \u201cLarge\u201d numbers will be different:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ seq 25 25 100 | shuf | head -2\n50\n25<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"target-number\">Target number<\/h2>\n\n\n\n<p>We could use a similar method with <code>seq<\/code> to list the numbers from 101 to 999, and <code>shuf<\/code> and <code>head<\/code> to randomize the list and print just one number, but that\u2019s a lot of numbers to print out. But since we just need to generate <em>one<\/em> random number, we can use another way to get a random value in that range.<\/p>\n\n\n\n<p>The Bash shell will generate a new random value with the <code>RANDOM<\/code> variable. Every time you reference this variable, Bash will expand it to be a random number in the range 0 to 32,767:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo $RANDOM\n10884\n$ echo $RANDOM\n23346<\/code><\/pre>\n\n\n\n<p>Bash also supports simple arithmetic operations, using the <code>$((<\/code> and <code>))<\/code> expansion. One of these arithmetic operators is <em>modulo<\/em> (<code>%<\/code>) or the remainder after division. That means <em>7 modulo 3<\/em> is the remainder after dividing 7 by 3, which is 2 with a <em>remainder of 1<\/em> (the modulo is 1). The range 101 to 999 is 899 numbers (including the first and last numbers in the list, just as the range 3 to 5 is three numbers: 3, 4, and 5) so let\u2019s start by calculating the modulo of a random number by 899:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo $(( RANDOM % 899 ))\n380<\/code><\/pre>\n\n\n\n<p>Note that arithmetic expansion in Bash automatically expands any variables between the <code>$((<\/code> and <code>))<\/code>, so we don\u2019t need to write it as <code>$RANDOM<\/code>, but can just use <code>RANDOM<\/code> since the arithmetic expansion will reference the value of <code>RANDOM<\/code> anyway.<\/p>\n\n\n\n<p>The modulo of 899 will return numbers from 0 to 898, but we want numbers from 101 to 999. That means we need to add 101 to the modulo, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo $(( RANDOM % 899 + 101 ))\n493<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"putting-it-all-together\">Putting it all together<\/h2>\n\n\n\n<p>With these pieces to print out four random \u201cSmall\u201d numbers and two \u201cLarge\u201d numbers, we can write a script to do this every time we want to play the Numbers game:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\necho \"The numbers are:\"\nseq 25 25 100 | shuf | head -2\n(seq 10; seq 10) | shuf | head -4\n\necho \"The target number is:\"\necho $(( RANDOM % 899 + 101 ))<\/code><\/pre>\n\n\n\n<p>This includes some extra <code>echo<\/code> statements to label the output for us, but otherwise this is what we wrote above for the \u201cSmall\u201d and \u201cLarge\u201d numbers. Save this into a file named <code>numbers<\/code> and every time you want to play the game, just run the script like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ bash numbers\nThe numbers are:\n75\n100\n4\n8\n3\n6\nThe target number is:\n452<\/code><\/pre>\n\n\n\n<p>By the way, I can get close to that, but I don\u2019t think I can get it exactly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4 x 100 = 400\n8 x 3 = 24\n75 - 24 = 51\n\n400 + 51 = 451<\/code><\/pre>\n\n\n\n<p>Let\u2019s try it again with a new list of numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ bash numbers\nThe numbers are:\n50\n100\n2\n7\n4\n8\nThe target number is:\n457<\/code><\/pre>\n\n\n\n<p>That one is easy and just requires a few steps to solve:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4 x 100 = 400\n50 + 7 = 57\n\n400 + 57 = 457<\/code><\/pre>\n\n\n\n<p>Try this fun game for yourself and see how close you can get to your target number.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use these commands to generate random numbers for a fun math quiz game.<\/p>\n","protected":false},"author":33,"featured_media":4314,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,69],"tags":[151,104,147,91],"class_list":["post-5852","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-fun","tag-bash","tag-command-line","tag-fun","tag-linux"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5852"}],"version-history":[{"count":2,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5852\/revisions"}],"predecessor-version":[{"id":6007,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5852\/revisions\/6007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4314"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}