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.