200 coin flips

16 11 2009

So I was reading a reddit posting and the article was about flipping a coin 200 times. I thought that would be a great opportunity to post a quick explanation on how I would go about making a coin flip 200 times without wasting a lot of time. So here it is a Powershell one-liner to flip 200 virtual quarters.

$i,$h,$t=0; while($i -lt 200){$coin = (Get-Random 2); if($coin -eq 0){$h++}else{$t++}$i++} $h;$t

Explanation

  • First we make a increment variable called $i, then we make $h (heads) and $t (tails) and set them to zero.
  • Then we setup how many coins we want to flip: while ($i -lt 200), while the increment variable $i is -lt (less than) 200 we keep flipping
  • Next we flip the coin with Get-Random 2, this gets a random number that has two possibilities (a zero or a one)
  • Then we see if the coin is a heads or a tails with the if statement
  • Then we add one to the heads or tails value
  • Finally we print out the results

There we have it a really quick way to flip two hundred “coins” in powershell

-Matt

Advertisement

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.