Linux shell implementation of random number of multiple methods (DATE,RANDOM,UUID) __linux

Source: Internet
Author: User
Tags uuid

Http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html

In daily life, random numbers are actually often encountered, want to throw dice, lottery, and draw lots. Oh, very simple can be achieved. So in doing programming, really to design a random number through their own program that is really not easy. Now a lot of the operating system kernel will provide the appropriate API, these raw parameters are to get some computer to run the original information, such as memory, voltage, physical signal, etc., its value in a time period can be guaranteed to be unique. All right, I won't say any more nonsense. Oh.

Shell Scripting Programs We have those that get random numbers.

one, through time to obtain random number (date)

This is what we often use, it can be said that time is the only, and will not repeat, from the inside to get the unique value of the same time. Adapt to all the programs inside.

Example:

1 2 3 4 5 6 7 8 9 10 11 12-13 [Chengmo@centos5 shell]$ date +%s 1287764773 #获得时间戳, current to: 1970-01-01 00:00:00 seconds apart #如果用它做随机数, the same second data is the same.   In doing cyclic processing, multithreading inside the basic can not meet the requirements. [Chengmo@centos5 shell]$ date +%n 738710457 #获得当前时间的纳秒数据, accurate to one-seconds. #这个相当精确了, even in multiple CPUs, a large number of loops inside, the same second, it is difficult to have the same results, but at different times there will be a lot of repeated collisions [chengmo@centos5 shell]$ date +%s%n 1287764807051101270 # This can be said to be more perfect, adding a time stamp, plus a nanosecond

By the above instructions, use it to do the base of random numbers, and then we see how to get a piece of data how to obtain random numbers.

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.