Science and technology

# Play a enjoyable math sport with Linux instructions

Like many individuals, I have been exploring a number of new TV reveals in the course of the pandemic. I not too long ago found a British sport present referred to as Countdown, the place contestants play two sorts of video games: a phrases sport, the place they attempt to make the longest phrase out of a jumble of letters, and a numbers sport, the place they calculate a goal quantity from a random number of numbers. Because I get pleasure from arithmetic, I’ve discovered myself drawn to the numbers sport.

The numbers sport generally is a enjoyable addition to your subsequent household sport night time, so I wished to share my very own variation of it. You begin with a group of random numbers, divided into “small” numbers from 1 to 10 and “large” numbers which can be 15, 20, 25, and so forth till 100. You decide any mixture of six numbers from each massive and small numbers.

Next, you generate a random “target” quantity between 200 and 999. Then use easy arithmetic operations together with your six numbers to attempt to calculate the goal quantity utilizing every “small” and “large” quantity not more than as soon as. You get the very best variety of factors for those who calculate the goal quantity precisely and fewer factors if you may get inside 10 of the goal quantity.

For instance, in case your random numbers have been 75, 100, 2, three, Four, and 1, and your goal quantity was 505, you may say 2+three=5, 5×100=500, Four+1=5, and 5+500=505. Or extra immediately: (2+three100 + Four + 1 = 505.

## Randomize lists on the command line

I’ve discovered one of the simplest ways to play this sport at house is to tug 4 “small” numbers from a pool of 1 to 10 and two “large” numbers from multiples of 5 from 15 to 100. You can use the Linux command line to create these random numbers for you.

Let’s begin with the “small” numbers. I would like these to be within the vary of 1 to 10. You can generate a sequence of numbers utilizing the Linux `seq` command. You can run `seq` a couple of other ways, however the easiest type is to offer the beginning and ending numbers for the sequence. To generate an inventory from 1 to 10, you may run this command:

```To randomize this listing, you need to use the Linux `shuf` (“shuffle”) command. `shuf` will randomize the order of no matter you give it, often a file. For instance, for those who ship the output of the `seq` command to the `shuf` command, you’ll obtain a randomized listing of numbers between 1 and 10:

\$ seq 1 10 | shufthree6eight107Four5219
To choose simply 4 random numbers from an inventory of 1 to 10, you’ll be able to ship the output to the `head` command, which prints out the primary few traces of its enter. Use the `-Four` choice to specify that `head` ought to print solely the primary 4 traces:
\$ seq 1 10 | shuf | head -Four61eightFour
Note that this listing is totally different from the sooner instance as a result of `shuf` will generate a random order each time.
Now you’ll be able to take the following step to generate the random listing of “large” numbers. The first step is to generate an inventory of attainable numbers beginning at 15, incrementing by 5, till you attain 100. You can generate this listing with the Linux `seq` command. To increment every quantity by 5, insert an alternative choice for the `seq` command to point the step:
\$ seq 15 5 1001520253035404550556065707580859095100
And simply as earlier than, you’ll be able to randomize this listing and choose two of the “large” numbers:
\$ seq 15 5 100 | shuf | head -27540
Generate a random quantity with Bash
I suppose you might use an identical methodology to pick out the sport’s goal quantity from the vary 200 to 999. But the best answer to generate a single random worth is to make use of the `RANDOM` variable immediately in Bash. When you reference this built-in variable, Bash generates a big random quantity. To put this within the vary of 200 to 999, you’ll want to put the random quantity into the vary zero to 799 first, then add 200.
To put a random quantity into a selected vary beginning at zero, you need to use the modulo arithmetic operation. Modulo calculates the the rest after dividing two numbers. If I began with 801 and divided by 800, the result’s 1 with a the rest of 1 (the modulo is 1). Dividing 800 by 800 provides 1 with a the rest of zero (the modulo is zero). And dividing 799 by 800 ends in zero with a the rest of 799 (the modulo is 799).
Bash helps arithmetic growth with the `\$(( ))` assemble. Between the double parentheses, Bash will carry out arithmetic operations on the values you present. To calculate the modulo of 801 divided by 800, then add 200, you’ll sort:
\$ echo \$(( 801 % 800 + 200 ))201
With that operation, you’ll be able to calculate a random goal quantity between 200 and 999:
\$ echo \$(( RANDOM % 800 + 200 ))673
You may marvel why I used `RANDOM` as a substitute of `\$RANDOM` in my Bash assertion. In arithmetic growth, Bash will robotically develop any variables throughout the double parentheses. You do not want the `\$` on the `\$RANDOM` variable to reference the worth of the variable as a result of Bash will do it for you.
Playing the numbers sport
Let’s put all that collectively to play the numbers sport. Generate two random “large” numbers, 4 random “small” values, and the goal worth:
\$ seq 15 5 100 | shuf | head -275100\$ seq 1 10 | shuf | head -FourFourthree102\$ echo \$(( RANDOM % 800 + 200 ))868
My numbers are 75, 100, Four, three, 10, and 2, and my goal quantity is 868.
I can get near the goal quantity if I do these arithmetic operations utilizing every of the “small” and “large” numbers not more than as soon as:
10×75 = 750
750+100 = 850and:Four×three = 12850+12 = 862862+2 = 864
That’s solely 4 away—not unhealthy! But I discovered this method to calculate the precise quantity utilizing every random quantity not more than as soon as:
Four×2 = eight
eight×100 = 800and:75-10+three = 68800+68 = 868
Or I may carry out these calculations to get the goal quantity precisely. This makes use of solely 5 of the six random numbers:
Four×three = 12
75+12 = 87and:87×10 = 870870-2 = 868
Give the Countdown numbers sport a strive, and tell us how effectively you probably did within the feedback.
```

To Top