I decided it was time to learn, or improve some basic scripting skills. I came up with a little project to work on for practice.

I would appreciate any advice. The script follows, then some output, and finally my questions.

The project is to write a script that flips a coin X number. The number of times heads or tails occurs in a row should be recorded. Those results should graphable or converted to a format which could be graphed (like CVS) By the way I don't have the slightest idea how to do this last part. But this is what we have so far.

This flips the coin and records the result

#!/bin/bash

#

# The plan is to write a shell scripts that flips a coin X times

# and records how many times head or tails

# occur in a row. And then perhaps to graph the out put

#

# create some files and setup some variable

touch coinflips

> coinflips

COUNTER=$1

tails=0

heads=0

# generate random number as many times as specified on command line.

# Then just take the last digit

until [ $COUNTER -lt 0 ]; do

for NUMROLL in `echo $RANDOM | sed -e "s/^.*\(.\)$/\1/"`

do

if [ $((NUMROLL % 2)) -eq 0 ]; then

# if it is even then add 1 to the count of variable heads

tails=0

heads=$((heads+1))

echo "Heads are $heads in a row" >> coinflips

else

# otherwise add 1 to tails

heads=0

tails=$((tails+1))

echo "Tails are $tails in a row">> coinflips

fi

let COUNTER-=1

done

done

------------------------------------------------------------------------

example output

[email protected]:~/scripts$ ./rollit 10

[email protected]:~/scripts$ cat coinflips

Tails are 1 in a row

Heads are 1 in a row

Tails are 1 in a row

Tails are 2 in a row

Tails are 3 in a row

Heads are 1 in a row

Tails are 1 in a row

Tails are 2 in a row

Heads are 1 in a row

Heads are 2 in a row

Tails are 1 in a row

--------------------------------------------------------------

Questions:

1. It should really only record the highest number of times heads or tails occured for each toss. For example in the output where it says Tails 1 then tails 2 then tails 3; only the tails 3 should exist. Leaving the others could be statistically misleading in our graph. I am not sure what I am doing wrong there.

2. Is there anything I am doing that could be done more efficiently or more correctly.

3. There anyway to do graphing in bash. Theoretically this should just create a perfect gausian curve. This must be possible as I have seen old time ascii art. Perhaps a lost art.

4. There seem to be 11 flips not 10 as $1 requested, Why?

5 If anyone wants to recommend any other learning projects please feel free.

Thanks for any advice. And sorry if this is the wrong place to post