Register
It is currently Thu Oct 02, 2014 4:24 am

Advice on beginner project requested


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Sep 22, 2011 1:18 pm   

Joined: Thu Sep 22, 2011 12:35 pm
Posts: 1
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
tom@lugh:~/scripts$ ./rollit 10
tom@lugh:~/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


Top
 Profile  
 PostPosted: Thu Sep 22, 2011 1:47 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi! This is a neat brush-up project :)

Here's what I would do to fix #1 -> You'll want to keep track of what the LAST flip was, and compare it with the CURRENT flip. Try adding a "LAST" variable to the mix, and checking to see if the current flip is the same as the last one before printing the "in a row" message.

For # 2, there's ALWAYS room to make things more efficient! As for 'correct'... I'm no expert... For instance, instead of using 'touch' to create the coinflip file, you *should* be able to simply use the '> coinfile' line to create the file if it doesn't exist or clear it if it does. Or you could make it more readable with something like "echo -n > coinflips".

You also might consider defining the output file in a variable, in case you want to use a different file later, you can change it in one place instead of three.

#3 Not sure! This would be interesting to investigate. I mean, you might be able to use something like MRTG or RRDTool, but I'm sure there's something simpler...

#4 Take a closer look at your 'until' loop. It might be obvious if you do the math on how many iterations you're asking for there :)

I hope this helps!
-J


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP