# Play a fun math game with Linux commands

## Play the numbers game from the popular British game show "Countdown" at home. Image credits :
João Trindade. Modified by Jason Baker. CC BY-SA 2.0.

## Subscribe now

Get the highlights in your inbox every week.

Like many people, I've been exploring lots of new TV shows during the pandemic. I recently discovered a British game show called Countdown, where contestants play two types of games: a words game, where they try to make the longest word out of a jumble of letters, and a numbers game, where they calculate a target number from a random selection of numbers. Because I enjoy mathematics, I've found myself drawn to the numbers game.

The numbers game can be a fun addition to your next family game night, so I wanted to share my own variation of it. You start with a collection of random numbers, divided into "small" numbers from 1 to 10 and "large" numbers that are 15, 20, 25, and so on until 100. You pick any combination of six numbers from both large and small numbers.

Next, you generate a random "target" number between 200 and 999. Then use simple arithmetic operations with your six numbers to try to calculate the target number using each "small" and "large" number no more than once. You get the highest number of points if you calculate the target number exactly and fewer points if you can get within 10 of the target number.

For example, if your random numbers were 75, 100, 2, 3, 4, and 1, and your target number was 505, you might say 2+3=5, 5×100=500, 4+1=5, and 5+500=505. Or more directly: (2+3100 + 4 + 1 = 505.

## Randomize lists on the command line

I've found the best way to play this game at home is to pull four "small" numbers from a pool of 1 to 10 and two "large" numbers from multiples of five from 15 to 100. You can use the Linux command line to create these random numbers for you.

Let's start with the "small" numbers. I want these to be in the range of 1 to 10. You can generate a sequence of numbers using the Linux `seq` command. You can run `seq` a few different ways, but the simplest form is to provide the starting and ending numbers for the sequence. To generate a list from 1 to 10, you might run this command:

```\$ seq 1 10
1
2
3
4
5
6
7
8
9
10```

To randomize this list, you can use the Linux `shuf` ("shuffle") command. `shuf` will randomize the order of whatever you give it, usually a file. For example, if you send the output of the `seq` command to the `shuf` command, you will receive a randomized list of numbers between 1 and 10:

```\$ seq 1 10 | shuf
3
6
8
10
7
4
5
2
1
9```

To select just four random numbers from a list of 1 to 10, you can send the output to the `head` command, which prints out the first few lines of its input. Use the `-4` option to specify that `head` should print only the first four lines:

```\$ seq 1 10 | shuf | head -4
6
1
8
4```

Note that this list is different from the earlier example because `shuf` will generate a random order every time.

Now you can take the next step to generate the random list of "large" numbers. The first step is to generate a list of possible numbers starting at 15, incrementing by five, until you reach 100. You can generate this list with the Linux `seq` command. To increment each number by five, insert another option for the `seq` command to indicate the step:

```\$ seq 15 5 100
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100```

And just as before, you can randomize this list and select two of the "large" numbers:

```\$ seq 15 5 100 | shuf | head -2
75
40```

## Generate a random number with Bash

I suppose you could use a similar method to select the game's target number from the range 200 to 999. But the simplest solution to generate a single random value is to use the `RANDOM` variable directly in Bash. When you reference this built-in variable, Bash generates a large random number. To put this in the range of 200 to 999, you need to put the random number into the range 0 to 799 first, then add 200.

To put a random number into a specific range starting at 0, you can use the modulo arithmetic operation. Modulo calculates the remainder after dividing two numbers. If I started with 801 and divided by 800, the result is 1 with a remainder of 1 (the modulo is 1). Dividing 800 by 800 gives 1 with a remainder of 0 (the modulo is 0). And dividing 799 by 800 results in 0 with a remainder of 799 (the modulo is 799).

Bash supports arithmetic expansion with the `\$(( ))` construct. Between the double parentheses, Bash will perform arithmetic operations on the values you provide. To calculate the modulo of 801 divided by 800, then add 200, you would type:

```\$ echo \$(( 801 % 800 + 200 ))
201```

With that operation, you can calculate a random target number between 200 and 999:

```\$ echo \$(( RANDOM % 800 + 200 ))
673```

You might wonder why I used `RANDOM` instead of `\$RANDOM` in my Bash statement. In arithmetic expansion, Bash will automatically expand any variables within the double parentheses. You don't need the `\$` on the `\$RANDOM` variable to reference the value of the variable because Bash will do it for you.

## Playing the numbers game

Let's put all that together to play the numbers game. Generate two random "large" numbers, four random "small" values, and the target value:

```\$ seq 15 5 100 | shuf | head -2
75
100
\$ seq 1 10 | shuf | head -4
4
3
10
2
\$ echo \$(( RANDOM % 800 + 200 ))
868```

My numbers are 75, 100, 4, 3, 10, and 2, and my target number is 868.

I can get close to the target number if I do these arithmetic operations using each of the "small" and "large" numbers no more than once:

```10×75 = 750
750+100 = 850

and:

4×3 = 12
850+12 = 862
862+2 = 864```

That's only four away—not bad! But I found this way to calculate the exact number using each random number no more than once:

```4×2 = 8
8×100 = 800

and:

75-10+3 = 68
800+68 = 868```

Or I could perform these calculations to get the target number exactly. This uses only five of the six random numbers:

```4×3 = 12
75+12 = 87

and:

87×10 = 870
870-2 = 868```

Give the Countdown numbers game a try, and let us know how well you did in the comments.