Bash Shell Generate Random Numbers

How do I create or generate random numbers (sequence of numbers that lack any pattern), under Bash shell?

The bash shell offers $RANDOM variable (it also works with ksh). From the bash man page:

Each time this is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.

To view $RANDOM, enter:
$ echo $RANDOM
Sample outputs:

11799

You can use the bash loop as follows to test random numbers:

for i in {1..5}; do echo $RANDOM; done

Sample outputs:

32340
18591
32100
15165
19743

You can store it to a variable as follows:

#!/bin/bash
n=$RANDOM
echo "My move: trying $n..."
 

You can have a random integer number within a certain range as follows:

 
n=$RANDOM
# display a random integer <= 200
echo $(( r %= 200 ))
# display random number between 100 and 200.
echo $((RANDOM%200+100))
 

Here is a sample shell script to find out random TCP port:

#!/bin/bash
# Set a trap to detect spam bots at port 80
 
# Find out random unused TCP port
findRandomTcpPort(){
port=$(( 100+( $(od -An -N2 -i /dev/random) )%(1023+1) ))
while :
do
(echo >/dev/tcp/localhost/$port) &>/dev/null && port=$(( 100+( $(od -An -N2 -i /dev/random) )%(1023+1) )) || break
done
echo "$port"
}
 
p=$(findRandomTcpPort)
echo "Setting Honeypot @ port 80 and real Apache server at port $p..."
# setHoneypot 80
# setApache $p
 

Another shell script to setup a random wallpaper. You can call this one from your crontab:

#!/bin/bash
# get images
files=(/nas/download/share/fun/images/wallpapers/*.png)
# find out random one
n=${#files[@]}
wallpaper="${files[RANDOM % n]}"
# Kde3 command (may not work with kde4)
# dcop kdesktop KBackgroundIface setWallpaper "$wallpaper" 5
 
# Gnome command
gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$wallpaper"

Using /dev/urandom or /dev/random

The character special files /dev/random and /dev/urandom provide an interface to the kernel's random number generator. You can use /dev/urandom as follows:
$ od -vAn -N4 -tu4 < /dev/urandom
Sample outputs:

2494028411

You can use /dev/random as follows:
$ od -An -N2 -i /dev/random
Sample outputs:

62362

Recommended readings:

See the following man pages:
man 4 random
man bash
man od

Was this answer helpful?

 Print this Article

Also Read

Linux: Check For Memory Leaks In Programs

How do I check my C programs under Linux operating systems for memory leaks? How do I debug and...

CentOS / Redhat: Install KVM Virtualization Software

Required Packages You must install the following packages: kmod-kvm : kvm kernel module(s)...

nginx: Send Custom HTTP Headers

How do I send or set arbitrary HTTP headers using nginx web server? You need to use add_header...

Linux/UNIX: Configure OpenSSH To Listen On an IPv6 Address

How do I enable OpenSSH SSH server to listen on an IPv6 address under Linux or UNIX operating...

Used And Free Memory

What's going on? Linux is borrowing unused memory for disk caching. This makes it looks like you...