Multiple methods to implement random numbers using linuxshell

Source: Internet
Author: User
Linuxshell multiple methods for random number implementation. What methods do we use to obtain random number in shell script programs? 1. obtain a random number (date) through time, which is also frequently used. it can be said that time is unique and will not be repeated, and the unique value of the same time is obtained from it. Suitable for... linux shell to implement random number multiple methods shell script programs What methods do we have to obtain random number? 1. obtain a random number (date) through time, which is also frequently used. it can be said that time is unique and will not be repeated, and the unique value of the same time is obtained from it. Adapted to all programs. Www.2cto.com date + % s1287764773 get the timestamp. if the number of seconds between the current and: 00:00:00 is used as a random number, the data of the same second is the same. In loop processing, multithreading basically cannot meet the requirements. Date + % N738710457: obtain the data of the current time in nanoseconds, accurate to one thousandth of a second. Www.2cto.com is quite accurate. even in multiple CPUs and a large number of loops, it is difficult to produce the same results within the same second, however, there will be a large number of repeated collisions in different time periods: date + % s % N1287764807051101270. this can be said to be perfect, adding a timestamp, adding a nanosecond 2, using internal system variables ($ RANDOM) in fact, linux already provides a system environment variable, which is a random number directly. haha, I think it is a waste of time to learn this method !! Echo $ random31618echo $ RANDOM10001: This data is an integer less than or equal to 5 digits. 3. a random number is generated using the unique data in the system (/dev/random, urandom) we know that there are some default linux devices under the dev directory. it gives us the feeling that the corresponding files are stored on the keyboard, hard disk, optical drive, and other devices. In fact, some linux devices are very special and have special purposes. We mentioned earlier:/dev/[udp | tcp]/host/port is special. Oh, it's a long journey. The/dev/random device stores real-time data of the current operating environment of the system. It can be regarded as the unique value data at a time in the system, so it can be used as the random number metadata. We can read the data in a file. The device data of/dev/urandom is the same as that of random. However, it is a non-blocking random number generator, and read operations will not cause blocking. Head-1/dev/urandom directly output garbled characters. In fact, it stores real-time data through binary data. how can we convert it into integer data? Head-200/dev/urandom | cksum 1615228479 50333 because urandom has a large amount of data, it cannot be read directly through cat. Here we take the first 200 rows. In fact, the entire data is changed, the same number is unique. Cksum reads the file content and generates a unique integer data. the generated result will not change only when the file content remains unchanged, and php crc function head-200/dev/urandom | cksum | cut-f1-d "" 484750180 cut is separated by "", and then the first split field data is obtained to obtain integer data, then, a method similar to one can obtain a random number. Topic: in the program, we often obtain the unique md5 value, followed by a string. if you want to represent it as an integer, you can use the crc function. crc is a cyclic redundancy check. when the same data is computed, a string of integer data is obtained. Currently, this verification is widely used. For details, refer to crc. there is another method below to read the generated uuid code from the device directly. 4. before reading the uuid code of linux, there is a concept: What is uuid? UUID is a universal Unique Identifier (UUID). It is a standard for Software construction and also known as the Open Software Foundation (OSF) is a part of the organization in the Distributed Computing Environment (Distributed Computing Environment, DCE) field. The purpose of UUID is to allow all elements in the distributed system to have unique identification information without specifying the identification information through the central control terminal. In this way, each user can create a UUID that does not conflict with others. In this case, duplicate names are not required during database creation. It will make the uuid generated by any computer on the network be unique across the Internet server network. The original information is added to hardware, time, and current running information of the machine. UUID format: contains 32 16-digit numbers. The "-" connection number is divided into five segments, in the format of 8-4-4-12 32 characters. Example: 550e8400-e29b-41d4-a716-446655440000. Therefore, the theoretical total number of UUID is 216x8 = 2128, which is approximately 3.4x1038. That is to say, if 1 mb uuid is generated every second, it takes 10 billion years to use up all UUID. In fact, when designing databases, you must have heard of guid (globally unique identifier) code, which is similar to uuid and supported by Microsoft. The code here is basically generated by the operating system kernel. In windows, you can easily obtain the uuid code, regardless of the database or other software. Linux uuid code is also provided by the kernel, in the/proc/sys/kernel/random/uuid file. In fact, there are many other files in the random directory, which are related to the uuid generation. Cat/proc/sys/kernel/random/uuid dff68213-b700-4947-87b1-d9e640334196 cat/proc/sys/kernel/random/uuid 7b57209a-d285-4fd0-88b4-9d3162d2e1bc 2 times in a row read, the uuid obtained is different cat/proc/sys/kernel/random/uuid | cksum | cut-f1-d "" 2141807556. the random integer obtained from the preceding method is in linux, several common positive random integer methods, except the first one, are different. In fact, the last three pseudo data sources that generate random codes are related to/dev/random devices.
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.