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
  • Subject: [mg122456] Re: Coin Toss Sim
  • From: Dan Dubin <ddubin at>
  • Date: Sat, 29 Oct 2011 07:09:56 -0400 (EDT)
  • Delivered-to:
  • References: <j89v28$8e$> <>

>"pooleman133" <poolebc221 at> schrieb im Newsbeitrag
>news:j89v28$8e$1 at
>>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}
>>   ]
>If I avoid RandomChoice it works fine:
>y[n_] := Block[{g, s},
>g := Table[If[Random[] < 1/2, "head", "tail"], {n}];
>s := Count[g, "head"]; {g, s}]
>{{"tail", "head", "head", "head", "tail", "tail",
>"tail", "head", "head", "tail"}, 3}
>-- Wolfgang

Its actually not working -- count the number of heads in the above 
output -- there are five. It was supposed to be three according to 
the value of s. The problem not RandomChoice, it is the use of 
delayed evaluation for g . g is being recreated with new random 
choices every time you call it, so the value of s that is created by 
the module has no relation to the list g that is output.
| Professor Dan Dubin
| Dept of Physics , Mayer Hall Rm 3126,
| UC San Diego La Jolla CA 92093-0319
| phone (858) - 534-4174 fax: (858)-534-0173
| ddubin at

  • Prev by Date: Re: List plot with random size for each point
  • Next by Date: Re: Export list to Excel without brackets
  • Previous by thread: Re: Coin Toss Sim
  • Next by thread: Re: Coin Toss Sim