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

Nginx Block And Deny IP Address OR Network Subnets

How do I block or deny access based on the host name or IP address of the client visiting...

iptables: Unknown error 18446744073709551615

When trying to run an OpenVPN Server and you receive the following error: iptables: Unknown...

How to change the root Password using SSH

Changing your Root Password Using SSH This article will show you how to change the root...

yum update Linux Error: Missing Dependency: xen-libs

When I type "yum update" under RHEL 5.x server I get the following error: Error: Missing...

PHP Security: Limit Resources Used By Script

How do I control and put limits on php scripts such as maximum execution time of each script and...