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.
vmstat is a tool for viewing virtual memory usage. General format: vmstat [-V] [-n] [delay [count]]C
GRUB is the default bootloader for most Linux systems. You can choose to enter different operating s
Preparation before erection: To set up a DNS server, you must install a software program that provi
Linux Mint is an Ubuntu-based distribution whose goal is to provide a more complete and immediate ex
More effective management Five cool Linux command line tricks
Apache configuration virtual host +SSL
Composition and installation of C development environment under Linux operating system
Tip: Linux personalized face clock display
Linux command must learn 60 installation and login commands
Linux dirs command to display records in the directory stack
Rookie Classroom: Several programming languages under Linux that are ignored
Getting Started with Linux Commands: Other Important Commands Using Basic Tutorial
Yum installation error: Error: database disk image is malformed Solution
Getting Started with Linux Commands: Other Important Commands Using Basic Tutorial
The SD card is damaged, how to take out the information inside
Win7 IIS 7 ASP gets the system date format as a slash solution
Win7/Win8.1 upgrade Win10 official version of the common problem collection
How does Win 8 view component services?
Netbook installation method of Windows7 system
Win7 Home Basic Edition upgrade win7 Ultimate system when prompted to upgrade failure analysis
Dual graphics card notebook win7 system to detect the problem of discrete graphics card