Last Updated: February 25, 2016
·
1.573K
· erebusbat

Quickly get Random.org integers from shell

I often have the need for a quick random number. Usually I just visit http://random.org/ and fill in the form. This works, but it is slow as I spend ALOT of my time at the shell. So I whipped up the following quick script that will generate 10 random numbers 1000..9999 which is my usual need. However the script allows me to quickly override the defaults in the times that I need to.

#!/bin/zsh
R_MIN=1000
R_MAX=9999
COLS=2
COUNT=10

if [[ $1 =~ '^-+h' ]]; then
    echo "USAGE:"
    echo "  $0 MIN MAX COUNT COLS"
    echo ""
    echo "If you specify an option then all preceding options MUST be specified"
    echo "DEFAULTS:"
    echo "    MIN=${R_MIN}"
    echo "    MAX=${R_MAX}"
    echo "  COUNT=${COUNT}"
    echo "   COLS=${COLS}"
    return 1
fi

# Parse options:
#  rnd MIN MAX COUNT COLS
if [ $# -ge 1 ]; then R_MIN=$1; fi
if [ $# -ge 2 ]; then R_MAX=$2; fi
if [ $# -ge 3 ]; then COUNT=$3; fi
if [ $# -ge 4 ]; then  COLS=$4; fi

echo "Contacting RANDOM.ORG to generate ${COUNT} random numbers ${R_MIN}..${R_MAX}, please wait...."
curl "https://www.random.org/integers/?num=${COUNT}&min=${R_MIN}&max=${R_MAX}&col=${COLS}&base=10&format=plain&rnd=new"

This uses ZSH specifics, if you want to change the help check on line 7 then it will probably be shell agnostic.