Tuesday, June 6, 2017

macOS Sierra: Bash (Unix shell): Generating Random Number in Arbitrary Range

Command (Random number between 0 to 120,000,000):

$ MIN=0; MAX=120000000; while rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w${#MAX} | head -1 | sed 's/^0*//;'); [ -z $rnd ] && rnd=0; (( $rnd < $MIN || $rnd > $MAX)); do :; done; echo $rnd;


Result:

74774521