Some thought analysis of generating random Numbers in Linux Shell

  • 2020-05-10 23:23:06
  • OfStack

1. Use time-dependent random Numbers

Use the data command to generate nanosecond data for the output unixtimestamp(%s) and the current time (%N, accurate to 1 billionth of a second).

The former is guaranteed not to repeat the output in different seconds and the latter is guaranteed not to repeat the number in the same second


date +%s%N

This method does not depend on the system as long as the time does not stop, 1 straight can be not repeated as a random number

2. System shell internal variable $RANDOM

  relies on shell

3. The checksum is obtained by Linux's random device and then calculated


head -10 /dev/urandom | cksum

And notice that you don't want to use cat to get this is a road that has no end and it doesn't make much difference between getting 1 row and getting 10 rows

4. Get through UUID of Linux's kernel

The computing information of UUID will add hardware, time, current running information of the machine, etc., which can ensure that the generated information is the only one in the world directly


cat /proc/sys/kernel/random/uuid

You can then convert the checksum to a number, depending on the Linux kernel.

Above 4 kinds of train of thought offer oneself reference


Related articles: