Narumi Tamaki |
Tuesday, September 6, 2016
Crontab: Tweet Random Line without Modulo Bias
Command:
$ crontab -l
Result:
*/30 * * * * perl -le 'sleep rand 360' && csv="list.csv"; wc=$[`wc -l < $csv` - 1]; rand=$[`./rand_nobias.sh $wc`]; head -$[$[$rand \% $wc] + 2] $csv | tail -1 | /usr/local/bin/gawk -f formatcsv.awk | ruby tweet.rb
ラベル:
Cron
Bourne shell: Generating Random Number without Modulo Bias
Command:
$ cat rand_nobias.sh
Result:
#!/bin/sh
$ 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;
Friday, September 2, 2016
Thursday, September 1, 2016
Subscribe to:
Posts (Atom)