Questions
1. How random numbers are obtained
2. How to determine the size of the random value is what we need
Answers
1. Can now be obtained by obtaining the number of milliseconds of the system time, after all, the number of milliseconds changes relatively fast
Can see this speed or change very quickly, take to do random is a good choice
2. For example, we need to get 1 to 39 random numbers, what should we do? Get the number of milliseconds divided by 39? However, the number obtained must not be within 1 to 39.
But we can get it by taking the remainder method.
#!/bin/bashnum=$ (date +%n); A= ' expr $num% ' echo $a
So we get all the values and test it.
There are 0, but 0 we don't need, so we should add 1 to the result.
#!/bin/bashnum=$ (date +%n), a= ' expr $num% ' b= ' expr $a + 1 ' echo $b
This makes it possible to generate random numbers from 1 to 39.
Test it.
CENTOS7 shell script Implementation random number