Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to play an interesting math game with Linux command

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use Linux commands to play an interesting math game, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can get something.

Play numbers on the popular British game show "Countdown" at home.

Like many people, I watched a lot of new TV programs during the pandemic. I recently discovered a British game show called Countdown, in which contestants play two games: a word game in which they try to find the longest word from a jumble of letters, and a numbers game in which they calculate a target number from a randomly selected number. Because I like math, I find myself attracted by number games.

Digital games can add fun to your next family game night, so I'd like to share one of my own game variants. You start with a set of random numbers, divided into "small" numbers from 1 to 10 and 15, 20, 25, and so on, until 100 "big" numbers. You choose any combination of six numbers from big numbers and small numbers.

Next, you generate a random "target" number between 200 and 999. Then do a simple arithmetic operation with your six numbers, and try to calculate the target number with each "small" and "large" number, but use it no more than once. If you can accurately calculate the target number, you can get the highest score, and if you are within 10 of the target number, you get a lower score.

For example, if your random number is 75, 100, 2, 3, 4 and 1, and your target number is 505, you can say 2, 3, 5, 5, 100, 500, 5, 5, 5, 500, 5, 5, 5, 500, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 500, 5, 5, 5, 5, 500, 5, 5, 5, 500, 5, 5, 5, 500, 500, 500, 505. Or more directly: (2: 3) × 100 + 4 + 1 = 505.

Randomize the list on the command line

I found that the best way to play this game at home is to draw four "small" numbers from a pool of 1 to 10 and two "big" numbers from a multiple of 15 to 100. You can use the Linux command line to create these random numbers for you.

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

$seq 1 1012345678910

To randomize this list, you can use Linux's shuf ("shuffle") command. Shuf will randomize the order of things 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 list of random numbers between 1 and 10:

$seq 1 10 | shuf36810745219

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

$seq 1 10 | shuf | head-46184

Note that this list is different from the previous example because shuf generates a random order each time.

Now you can take the next step to generate a random list of "large" numbers. The first step is to generate a list of possible numbers, starting at 15 and incrementing by 5 until it reaches 100. You can use Linux's seq command to generate this list. To increment each number by 5, insert another option in the seq command to indicate the step:

$seq 15 5 1001520253035404550556065707580859095100

As before, you can randomize the list and select two "big" numbers:

$seq 155100 | shuf | head-27540 generate a random number with Bash

I think you can use a similar method to select the target number of the game from 200 to 999. But the easiest way to generate a single random number is to use the RANDOM variable directly in Bash. When you reference this built-in variable, Bash generates a large random number. To put it in the range of 200 to 999, you need to put the random number in the range of 0 to 799, and then add 200.

To put random numbers in a specific range starting at 0, you can use the modular arithmetic operator. The module calculates the remainder of the division of two numbers. If I divide 801 by 800, the result is 1 and the remainder is 1 (modulus is 1). The result of dividing 800 by 800 is 1, and the remainder is 0 (modulus is 0). The result of dividing 799 by 800 is 0, and the remainder is 799 (modulus is 799).

Bash supports arithmetic expansion through the $(()) structure. Between the parentheses, Bash will perform arithmetic operations on the values you provide. To calculate the modulus 801 divided by 800, and then add 200, you can type:

$echo $(801% 800,200) 201

By doing this, you can calculate a random target number between 200 and 999:

Echo $((RANDOM% 800 + 200)) 673

You may wonder why I use RANDOM instead of $RANDOM in the Bash statement. In arithmetic extensions, Bash automatically expands any variables in double parentheses. You don't need $on the $RANDOM variable to refer to the value of that variable, because Bash will do it for you.

Play a number game

Let's put all this together and play a numbers game. Produces two random "large" numbers, four random "small" values, and the target value:

$seq 15 5100 | shuf | head-275100$ seq 1 10 | shuf | head-443102$ echo $

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

If I do these arithmetic operations with each "small" and "big" number, not more than once, I can get close to the target number:

10 × 75 = 750750 / 100 = 850 and then: 4 × 3 = 12850 / 12 = 862862 / 2 = 864

There's only a difference of 4, not bad! But I found that it was possible to use each random number no more than once to calculate the exact number:

4 × 2 = 88 × 100 = 800 and then: 75-10 × 3 = 68800 × 68 = 868

Or I can do these calculations to get the target number accurately. This uses only five of the six random numbers:

4 × 3 = 1275 # 12 = 87 and then: 87 × 10 = 870870-2 = 868

Try the Countdown numbers game.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report