CENTOS7 shell script Implementation random number

Source: Internet
Author: User

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

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.