diff --git a/sources/tech/20210416 Play a fun math game with Linux commands.md b/sources/tech/20210416 Play a fun math game with Linux commands.md deleted file mode 100644 index cbb55f7f4e..0000000000 --- a/sources/tech/20210416 Play a fun math game with Linux commands.md +++ /dev/null @@ -1,209 +0,0 @@ -[#]: subject: (Play a fun math game with Linux commands) -[#]: via: (https://opensource.com/article/21/4/math-game-linux-commands) -[#]: author: (Jim Hall https://opensource.com/users/jim-hall) -[#]: collector: (lujun9972) -[#]: translator: (geekpi) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) - -Play a fun math game with Linux commands -====== -Play the numbers game from the popular British game show "Countdown" at -home. -![Math formulas in green writing][1] - -Like many people, I've been exploring lots of new TV shows during the pandemic. I recently discovered a British game show called _[Countdown][2]_, 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**+**3**)×**100** \+ **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. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/21/4/math-game-linux-commands - -作者:[Jim Hall][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/jim-hall -[b]: https://github.com/lujun9972 -[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/edu_math_formulas.png?itok=B59mYTG3 (Math formulas in green writing) -[2]: https://en.wikipedia.org/wiki/Countdown_%28game_show%29 diff --git a/translated/tech/20210416 Play a fun math game with Linux commands.md b/translated/tech/20210416 Play a fun math game with Linux commands.md new file mode 100644 index 0000000000..c4aad23a15 --- /dev/null +++ b/translated/tech/20210416 Play a fun math game with Linux commands.md @@ -0,0 +1,209 @@ +[#]: subject: (Play a fun math game with Linux commands) +[#]: via: (https://opensource.com/article/21/4/math-game-linux-commands) +[#]: author: (Jim Hall https://opensource.com/users/jim-hall) +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) + +用 Linux 命令玩一个有趣的数学游戏 +====== +在家玩流行的英国游戏节目 “Countdown” 中的数字游戏。 +![Math formulas in green writing][1] + +像许多人一样,我在大流行期间探索了许多新的电视节目。我最近发现了一个英国的游戏节目,叫做 _[Countdown][2]_,参赛者在其中玩两种游戏:一种是_单词_游戏,他们试图从杂乱的字母中找出最长的单词;另一种是_数字_游戏,他们从随机选择的数字中计算出一个目标数字。因为我喜欢数学,我发现自己被数字游戏所吸引。 + +数字游戏可以为你的下一个家庭游戏之夜增添乐趣,所以我想分享我自己的变化。你以一组随机数字开始,分为 1 到 10 的“小”数字和 15、20、25 的“大”数字,以此类推,直到 100。你从大数字和小数字中挑选六个数字的任何组合。 + +接下来,你生成一个 200 到 999 之间的随机“目标”数字。然后用你的六个数字进行简单的算术运算,尝试用每个“小”和“大”数字计算出目标数字,但使用不能超过一次。如果你能准确地计算出目标数字,你就能得到最高分,如果距离目标数字 10 以内就得到较低的分数。 + +例如,如果你的随机数是 75、100、2、3、4 和 1,而你的目标数是 505,你可以说 _2+3=5_,_5×100=500_,_4+1=5_,以及 _5+500=505_。或者更直接地:(**2**+**3**)×**100** \+ **4** \+ **1** = **505**. + +### 在命令行中随机化列表 + +我发现在家里玩这个游戏的最好方法是从 1 到 10 的池子里抽出四个“小”数字,从 15 到 100 的 5 的倍数中抽出两个“大”数字。你可以使用 Linux 命令行来为你创建这些随机数。 + +让我们从“小”数字开始。我希望这些数字在 1 到 10 的范围内。你可以使用 Linux 的 `seq` 命令生成一个数字序列。你可以用几种不同的方式运行 `seq`,但最简单的形式是提供序列的起始和结束数字。要生成一个从 1 到 10 的列表,你可以运行这个命令: + +``` +$ seq 1 10 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +为了随机化这个列表,你可以使用 Linux 的 `shuf`("shuffle")命令。`shuf` 将随机化你给它的东西的顺序,通常是一个文件。例如,如果你把 `seq` 命令的输出发送到 `shuf` 命令,你会收到一个 1 到 10 之间的随机数字列表: + + +``` +$ seq 1 10 | shuf +3 +6 +8 +10 +7 +4 +5 +2 +1 +9 +``` + +要从 1 到 10 的列表中只选择四个随机数,你可以将输出发送到 `head` 命令,它将打印出输入的前几行。使用 `-4` 选项来指定 `head` 只打印前四行: + + +``` +$ seq 1 10 | shuf | head -4 +6 +1 +8 +4 +``` + +注意,这个列表与前面的例子不同,因为 `shuf` 每次都会生成一个随机顺序。 + +现在你可以采取下一步措施来生成”大“数字的随机列表。第一步是生成一个可能的数字列表,从 15 开始,以 5 为单位递增,直到达到 100。你可以用 Linux 的 `seq` 命令生成这个列表。为了使每个数字以 5 为单位递增,在 `seq` 命令中插入另一个选项来表示_步进_: + + +``` +$ seq 15 5 100 +15 +20 +25 +30 +35 +40 +45 +50 +55 +60 +65 +70 +75 +80 +85 +90 +95 +100 +``` + +就像以前一样,你可以随机化这个列表,选择两个”大“数字: + + +``` +$ seq 15 5 100 | shuf | head -2 +75 +40 +``` + +### 用 Bash 生成一个随机数 + +我想你可以用类似的方法从 200 到 999 的范围内选择游戏的目标数字。但是生成单个随机数的最简单的方案是直接在 Bash 中使用 `RANDOM` 变量。当你引用这个内置变量时,Bash 会生成一个大的随机数。要把它放到 200 到 999 的范围内,你需要先把随机数放到 0 到 799 的范围内,然后加上 200。 + +要把随机数放到从 0 开始的特定范围内,你可以使用**模数**算术运算符。模数计算的是两个数字相除后的_余数_。如果我用 801 除以 800,结果是 1,余数是 1(模数是 1)。800 除以 800 的结果是 1,余数是 0(模数是 0)。而用 799 除以 800 的结果是 0,余数是 799(模数是 799)。 + +Bash 通过 `$(())` 结构支持算术扩展。在双括号之间,Bash 将对你提供的数值进行算术运算。要计算 801 除以 800 的模数,然后加上 200,你可以输入: + + + +``` +$ echo $(( 801 % 800 + 200 )) +201 +``` + +通过这个操作,你可以计算出一个 200 到 999 之间的随机目标数: + + +``` +$ echo $(( RANDOM % 800 + 200 )) +673 +``` + +你可能想知道为什么我在 Bash 语句中使用 `RANDOM` 而不是 `$RANDOM`。在算术扩展中, Bash 会自动扩展双括号内的任何变量. 你不需要在 `$RANDOM` 变量上的 `$` 来引用该变量的值, 因为 Bash 会帮你做这件事。 + +### 玩数字游戏 + +让我们把所有这些放在一起,玩玩数字游戏。产生两个随机的”大“数字, 四个随机的”小“数值,以及目标值: + + +``` +$ seq 15 5 100 | shuf | head -2 +75 +100 +$ seq 1 10 | shuf | head -4 +4 +3 +10 +2 +$ echo $(( RANDOM % 800 + 200 )) +868 +``` + +我的数字是 **75**、**100**、**4**、**3**、**10** 和 **2**,而我的目标数字是 **868**。 + +如果我用每个”小“和”大“数字做这些算术运算,并不超过一次,我就能接近目标数字了: + + +``` +10×75 = 750 +750+100 = 850 + +然后: + +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 了,不错!但我发现这样可以用每个随机数不超过一次来计算出准确的数字: + + +``` +4×2 = 8 +8×100 = 800 + +然后: + +75-10+3 = 68 +800+68 = 868 +``` + +或者我可以做_这些_计算来准确地得到目标数字。这只用了六个随机数中的五个: + + +``` +4×3 = 12 +75+12 = 87 + +然后: + +87×10 = 870 +870-2 = 868 +``` + +试一试 _Countdown_ 数字游戏,并在评论中告诉我们你做得如何。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/21/4/math-game-linux-commands + +作者:[Jim Hall][a] +选题:[lujun9972][b] +译者:[geekpi](https://github.com/geekpi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/jim-hall +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/edu_math_formulas.png?itok=B59mYTG3 (Math formulas in green writing) +[2]: https://en.wikipedia.org/wiki/Countdown_%28game_show%29