Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5906
| From | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Coin Toss Sim |
| Date | 2011-10-27 10:29 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j8bbqu$8i3$1@smc.vnet.net> (permalink) |
| References | <201110262139.RAA00063@smc.vnet.net> |
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@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@yahoo.com
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Coin Toss Sim DrMajorBob <btreat1@austin.rr.com> - 2011-10-27 10:29 +0000
csiph-web