MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Coin Toss Sim


On 10/26/11 at 5:39 PM, poolebc221 at gmail.com (pooleman133) wrote:

>I am new to mathematica and am trying to make a coin flipping
>simulation however mo code isn't working. When I tell it to count
>the number of head it gives me a bad count. The code I am using is
>listed below any help would be greatly appreciated.

>y[n_] := Block[{g, s},  g := RandomChoice[{"head", "tail"}, n];
>s := Count[g, "head"];
>{g, s}
>]

You don't say what you mean by a "bad count". When I run your
code it seems to be working as I would expect. That is:

In[1]:= y[n_] := Block[{g, s},
   g := RandomChoice[{"head", "tail"}, n];
   s := Count[g, "head"];
   {g, s}]

In[2]:= y[10]

Out[2]= {{tail,tail,head,tail,head,tail,head,head,tail,tail},6}

Note, I would implement this simulation as:

n = 100;
g = RandomInteger[1,n];
Total[g]

Assuming I needed I needed a set of heads/tails to do further
calculations on. If I only needed the sum, i.e., counts for
heads, I would code the simulation as

RandomInteger[BinomialDistribution[n,1/2]]

=46or small values of n, there won't be much difference between
these. But for very large values of n, I believe you will find
RandomInteger to be faster than RandomChoice.




  • Prev by Date: Re: A Problem seemingly with NIntegrate
  • Next by Date: Re: simple question about removing elements from a table
  • Previous by thread: Re: Coin Toss Sim
  • Next by thread: Re: Coin Toss Sim