A lot of times, a little hazard is good and also needed in our lives. There are many reasons you need random data, some of them are related to encryption, password creation, lottery numbers, read and write speed tests, etc.

Below is the table of contents of this tutorial:

  1. Generating Random Numbers Using Variables
  2. Generating Random Numbers Using shuf
  3. Generating Random Letters and Strings
  4. Generating Files Containing Random Data
  5. Conclusion

Generating Random Numbers Using Variables

For this part of the tutorial, I will use the $RANDOM variable. $RANDOM is an internal Bash function that returns a pseudorandom integer in the 0 – 32767 range. Doing a simple echo of this variable/function will throw a number:

$ echo $RANDOM
15221

But we need more than this, we need to be more specific most of the time. Let’s say we need to generate 10 random numbers. We can do this using a one-liner:

$ for((i=1;i<=10;i++)); do echo $(($RANDOM)); done

Let’s put a maximum limit on this range. Say you need to generate numbers from 0 to 30. For this, we can use modulo. If you want the high limit to include 30, you need to use 30 + 1, meaning 31.

$ for((i=1;i<=10;i++)); do echo $(($RANDOM%31)); done

As you can see, the numbers in the list are not unique. Also, they include 0, maybe you require 0 not to be part of the list. To get rid of 0, we need to add 1 to the list, but keep in mind that 1 is also added to the limit, so now you can use 30 instead of 31.

$ for((i=1;i<=10;i++)); do echo $(($(($RANDOM%30))+1)); done

We can put this in a file and use it as a CLI command. Put this code in a file, let’s call it randrange.sh:

#!/bin/bash
if [ $# -ne 3 ]; then
        echo "please enter 3 values: 1. how many numbers; 2.starting number; 3.ending number "
        exit 1
fi

NUMB=$(($3-$2+1))
RANDOM=$$
for i in `seq $1`
do
        N=$(($(($RANDOM%$NUMB))+$2))
        echo $N
done

Make the file executable – chmod +x randrange.sh. This short script will take 3 arguments and exit is a different number of arguments are used as input. For example, we need 6 numbers in the 1-49 range:

$ ./randrange.sh 6 1 49
19
48
27
33
22
6

Using this construction can provide an insight into the generating mechanism. What if you are lazy? Being lazy is not a bad thing in Linux. In this case, you can use the shuf utility.

Generating Random Numbers Using shuf

The official definition of shuff describes it as “a command-line utility included in the textutils package of GNU Core Utilities for creating a standard output consisting of random permutations of the input.”

Argument usage for shuf is as follows:

  -e, --echo                treat each ARG as an input line
  -i, --input-range=LO-HI   treat each number LO through HI as an input line
  -n, --head-count=COUNT    output at most COUNT lines
  -o, --output=FILE         write result to FILE instead of standard output
      --random-source=FILE  get random bytes from FILE
  -r, --repeat              output lines can be repeated
  -z, --zero-terminated     end lines with 0 byte, not newline
      --help     display this help and exit
      --version  output version information and exit

The simple usage is shuf -i LO-HI -n NUMBER COUNT. Translating the 6 from 1-49 above would be:

$ shuf -i 1-49 -n 6

If you need to save the output in a file, you can use the output redirection “>” symbol.

$ shuf -i 1-49 -n 6 > numbers.txt

The above command will write the 6 numbers to a numbers.txt file and it will not show the numbers on the screen. Use cat to view the content of the file:

Generating Random Letters and Strings

The use cases for random letters and strings are too many to be enumerated here, so I’ll just skip to the technical part. For this part of the tutorial, I will use the special /dev/urandom file.

Generate a random letter:

$ cat /dev/urandom | tr -dc 'A-Z' | fold -w 1 |head -n 1
F

Generate a random 10 character, UPPER CASE and lower case letters, string:

$ cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w 10 |head -n 1
bSqYlfGuQL

Generate a random complex 15 character string, using ASCII printable characters:

$ cat /dev/urandom | tr -dc "[:print:]" | fold -w 15 |head -n 1
[n!'zNHo)6c4mc8

Obviously, this can be used for password generation.

Generating Files Containing Random Data

Sometimes you need to generate a larger block of random data and put it in a file. I know I used it for testing disk write and read speed, but it can also be used in software developing and testing, compression algorithms, and many other scenarios.

I will present three ways of generating a 10 MB file containing random data.

A) Using dd: “dd if=input_file of=output_file bs=block_size count=number of blocks”

$ dd if=/dev/urandom of=random_dd.txt bs=10M count=1
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.107385 s, 97.6 MB/s

Then you can check the file using ls:

As you can see, additional information is presented – the time it took to write the file ( 0.107385 s ) and the writing speed ( 97.6 MB/s ).

B) Using shred: you first need to create the file and then “shred -n 1 -s 10M file_name”

$ touch shred_file.txt && shred -n 1 -s 10M shred_file.txt

then check the file:

$ ls -alh shred_file.txt

shred is an interesting utility mainly used to overwrite a file repeatedly, in order to make it harder for even very expensive hardware probing to recover the initial data contained by that file.

C) Using /dev/urandom: “head -c file_size/dev/urandom > file_name”

$ head -c 10M /dev/urandom > urandom_10M.txt

Conclusion

In a world more and more obsessed with security, using strong passwords, using in-transit, at-rest encryption, and randomizing access tokens is the only way to thrive. This tutorial presents a quick intro to the randomizing concepts and tools which you can use for fun things, like generating your lotto numbers, and for important actions, like software development and testing, generating a strong password for your Bitcoin wallet, or even shredding a file in a way no one can recover its initial content.