Sunday, May 23, 2021

Five ways to generate a random text on Linux

Just for fun- listing some ways to generate a random text in Linux.

1. Using builtin RANDOM function in bash. It generates random number less than 32767. Concat two of these numbers and convert the first 8-bytes of result into a base64 value. Gives a mix of upper and lowercase plus digits:
bjha@dell-3460:~$ echo ${RANDOM}${RANDOM} | head -c8 | base64
MzE3MTgyNzA=

2. Using /dev/urandum. Pick first 8-bytes of random data and encode as base64:

bjha@dell-3460:~$ head -c8 /dev/urandom | base64
OiC6S52snTk=

3. Similar to the previous one, but, using dd cmd instead of head. Read 8-bytes from urandum and base64 encode it:

bjha@dell-3460:~$ dd if=/dev/urandom bs=8 count=1 | base64
fB1s3WlcqdY=
...

4. Using date cmd. Print the date as epoch seconds (%s) and base64 encode the result

bjha@dell-3460:~$ date '+%s' | base64
MTYyMTc4NzczMwo=

5. Using uuidgen cmd

bjha@dell-3460:~$ uuidgen

7be03091-e088-4cef-ab2a-e484ac63d336