# Essays/Coin Flipping

What is your chance to get 3 heads in a row from ten coin flips?

There was an original blog post to answer that question with Monte Carlo simulation in Python. How can we do that in J?

## Random Coin Flips

Let's say 1 signifies head and 0 tail. Then we could easily roll a sequence of coin flipping as:

?.30\$ 2
0 1 0 1 1 1 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1

How do we count number of consecutive heads?

(* >:)/\.&.|. ?.30\$2
0 1 0 1 2 3 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 2 3 0 0 0 0 0 1 2

Notice the usage of /\.&.|., which is a common phrase in J to do operations accumulatively.

## Maximal number of consecutive heads

It's easy at this stage.

>./ (* >:)/\.&.|. ?.30\$2
3

Were there 3 heads in a row?

3<: >./ (* >:)/\.&.|. ?.30\$2
1

## Monte Carlo

How can we easily move what we've done to a multiple of experiments, that is Monte Carlo simulations?

i%~ +/ 3<: ([:>./(* >:)/\.&.|.)"1 ?.((i=.1000000), 10)\$2
0.508196

Notice the rank("1) applied to the previous expression to make it applied each row.

Originally written by June Kim. Variations and suggestions are welcome.