Monday, October 8, 2007

Flipping a coin in Bash

I took the day off to look for a new laptop. So I devised this clever little script to manage my time today.

#! /bin/bash

# Flip a coin to see if I should have another beer.
# experimentaly biased. :-)

HEAD=0
TAIL=0

for x in 1 2 3 4 5 6 7 8 9
do

FLIP=$(($(($RANDOM%10))%2))

if [ $FLIP -eq 1 ]
then
echo "TAIL"
TAIL=$(($TAIL+1))
else
echo "HEAD"
HEAD=$(($HEAD+1))
fi
done

if [ $HEAD -gt $TAIL ]
then
echo "BEER TIME.!"
else
echo "Your not entitled to a beer. Run $0 again."
fi

I am still looking for a new laptop.

2 comments:

Anonymous said...

brilliant!

Tech Spots said...

Sometimes decision-making feels stressful, but adding a little randomness helps.
This online coin flip site is a cool way to decide things in seconds.
It’s great for games, classroom activities, or just to make fun choices.
The sound and visual feel realistic, which makes it even better.