MathGroup Archive 2011

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

Search the Archive

Re: Coin Toss Sim


You're computing g twice, so the counts don't match.

(You used SetDelayed to define g, so RandomChoice is called again EVERY  
time g is mentioned.)

This fixes it:

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

or this:

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

or this:

y[n_] := {#, Count[#, "head"]} &@RandomChoice[{"head", "tail"}, n]

Block can be replaced by Module in either of the first two codes.

Bobby

On Wed, 26 Oct 2011 16:39:20 -0500, pooleman133 <poolebc221 at gmail.com>  
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}
>   ]
>


-- 
DrMajorBob at yahoo.com



  • Prev by Date: Re: runs slowly?
  • Next by Date: Re: Coin Toss Simulation
  • Previous by thread: Coin Toss Sim
  • Next by thread: Re: Coin Toss Sim