Re: Coin Toss Sim
- To: mathgroup at smc.vnet.net
- Subject: [mg122388] Re: Coin Toss Sim
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 27 Oct 2011 06:30:23 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
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.