The linux shell implements multiple methods of random numbers (date, random, uuid)

  
 

In daily life, random numbers are often encountered in real life. It is very simple to lose the dice, grab, and draw lots. So in programming, it is really not easy to design a random number through your own program. Now a lot of operating system
kernel will provide the corresponding api, these raw parameters are to get some computer to run raw information, such as memory, voltage, physical signals, etc., its value can be guaranteed in a period of time The only thing. Ok, I will not talk nonsense. Shell scripts We have those methods to get random numbers? First, get random numbers through time (
date)
This is also used by us. It can be said that time is unique and will not be repeated. From this, we can get the same time. Unique value. Adapted to all the programs inside. Example:

[barlow@centos6 shell]$ date +%s1287764773#Get timestamp, current to: 1970-01-01 00:00:00 Separated seconds# If used It does random numbers, and the same one second of data is the same. In the loop processing, the multi-threading basically can not meet the requirements. [barlow@centos6 shell]$ date +%N738710457#Get nanosecond data for the current time, accurate to one hundredth of a second. #This is quite accurate, even in a lot of cpu, a lot of loops, in the same second, it is difficult to see the same result, but there will be a lot of repeated collisions in different time [barlow@centos6 shell]$ date +%s%N1287764807051101270# This can be said to be perfect, adding a timestamp, plus nanoseconds

Through the above description, use it to do the base of the random number, then we will see how to get a piece of data Get a random number.

#!/bin/sh#Write a random function, call the method random min max# to get the random integer directly in min and max# Get the random number return value, update the value after calculating the random number in the shell function Function random(){min=$1;max=$2-$1;num=$(date +%s+%N);((retnum=num%max+min));#To perform the remainder operation, echo $retnum; # 这里e print out the value by echo, and then get the function, stdout can get the value # There is a return, define the full price variable, then the function changes the content, read outside } # get 1-10 seq data item for i In {1..10};doout=$(random 2 10000);echo $i,"2-10000",$out;done;

Look at the results:

[barlow@centos6 shell]$ sh testrandom.sh1,2-10000,56002,2-10000,52953,2-10000,34324,2-10000,31485,2-10000,90416,2-10000,42907 , 2-10000, 23808, 2-10000, 90099, 2-10000, 547410, 2-10000, 3664

In a loop, the values ​​are different. This is our common method, adapt to various languages, is a general algorithm, even if the server does not provide, the same unique data mark at a certain moment, we can also do its own pseudo-random number through this method. Here's a simpler way, don't do it ourselves. 2
, through internal system variables
($RANDOM)
In fact, Linux has provided a system environment variable, which is directly a random number, haha, I just learned The method is not in vain! !

[barlow@centos6 shell]$ echo $RANDOM10918[barlow@centos6 shell]$ echo $RANDOM10001# 2 consecutive visits, the result is different, this data is an integer less than or equal to 5 digits

There may be questions. How do you get more than 5 random numbers? Hehe, add a fixed 10-digit integer, and then make a surplus, the same as in Example 1. The next example is that we have done it ourselves.

Copyright © Windows knowledge All Rights Reserved