Tuesday, September 6, 2016

Bourne shell: Generating Random Number without Modulo Bias

Command: 

$ cat rand_nobias.sh


Result:

#!/bin/sh

numlines=$1;
randmax=32767;
max=$((($randmax/$numlines)*$numlines)); 
rand=$RANDOM;

until [ $rand -lt $max ] 
do 
rand=$RANDOM;
done 


echo $rand;