Random numbers with bash

I needed to get some quick random numbers that would represent a time on the form HH:SS. In my case the time should be between 10:00 and 12:55.

I did this in bash with:

#!/bin/bash
# If the random number is 1, then LAST-number is set to 0.
# Else it would be set to 5.
if [ $(( $RANDOM % 2 )) -eq 1 ];then
  LAST=0
else
  LAST=5
fi

# Print a number between  10 or 12,
# then a semicolon, then a number between 0-5,
# and finally either 0 or 5.
echo "0"$(( $RANDOM % 2 + 10 ))":"$(( $RANDOM % 6 ))$LAST

Leave a Reply

Your email address will not be published.