Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Coin Toss Sim

  • To: mathgroup at smc.vnet.net
  • Subject: [mg122376] Re: Coin Toss Sim
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 27 Oct 2011 06:28:12 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201110262139.RAA00063@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

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



  • References:
  • 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