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.