Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5942 > unrolled thread
| Started by | Heike Gramberg <heike.gramberg@gmail.com> |
|---|---|
| First post | 2011-10-27 10:46 +0000 |
| Last post | 2011-10-27 10:46 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Coin Toss Sim Heike Gramberg <heike.gramberg@gmail.com> - 2011-10-27 10:46 +0000
| From | Heike Gramberg <heike.gramberg@gmail.com> |
|---|---|
| Date | 2011-10-27 10:46 +0000 |
| Subject | Re: Coin Toss Sim |
| Message-ID | <j8bcqj$8ut$1@smc.vnet.net> |
You get a bad count because you're using SetDelayed (:=) to define
which has the effect that the r.h.s. of g is reevaluated every time you
call g. In this case that means that you generate a new list of coin
tosses whenever you call g. So when you call {g,s} you are in fact using
a different list to count the heads from the one you output. To solve
this you should use Set (=) for defining g instead, e.g.
y[n_] := Block[{g, s},
g = RandomChoice[{"head", "tail"}, n];
s = Count[g, "head"];
{g, s}]
Heike.
On 26 Oct 2011, at 23:39, 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}
> ]
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web