RHCE path-method for generating random numbers in Linuxshell

Source: Internet
Author: User
In Linux, how does one use commands to generate a random number in a range? In shell, there is an environment variable RANDOM with a range of 0--32767. If we want to generate a number in the range of 0 to 25, what should we do? As follows: $ RANDOM % 26 use this environment variable to modulo 26, and the minimum value is 0 and the maximum value is 25. If you want to get the number in the range of 1-68, you can get $ RANDOM % 68 + 1. We can get a RANDOM number with a minimum value of 0 and a maximum value of 67, plus 1.

In Linux, how does one use commands to generate a random number in a range?

There is an environment variable RANDOM in shell, which has the range of 0--32767

What should we do if we want to generate a number in the range of 0 to 25? As follows:

$ RANDOM % 26

Using this environment variable to modulo 26, the minimum value is 0 and the maximum value is 25.

If you want to get the number in the range of 1-68, you can do this.

$ RANDOM % 68 + 1. We can obtain a RANDOM number with a minimum value of 0 and a maximum value of 67. Then, we can naturally get a maximum value of 1 by adding 1, the minimum value is 68.

If you want to get the number in the range of 6--87. Yes.

$ RANDOM % 82 + 6. We can obtain a RANDOM number with a minimum value of 0 and a maximum value of 81. In addition to 6, we can naturally get a maximum value of 87, the minimum number is 6.

And so on. This method can be used to generate numbers in other ranges. The following three examples are used to summarize the rule.

And so on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.