Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5916 > unrolled thread
| Started by | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| First post | 2011-10-27 10:35 +0000 |
| Last post | 2011-10-27 10:35 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Coin Toss Sim Bill Rowe <readnews@sbcglobal.net> - 2011-10-27 10:35 +0000
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Date | 2011-10-27 10:35 +0000 |
| Subject | Re: Coin Toss Sim |
| Message-ID | <j8bc6f$8q3$1@smc.vnet.net> |
On 10/26/11 at 5:39 PM, poolebc221@gmail.com (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}
>]
You don't say what you mean by a "bad count". When I run your
code it seems to be working as I would expect. That is:
In[1]:= y[n_] := Block[{g, s},
g := RandomChoice[{"head", "tail"}, n];
s := Count[g, "head"];
{g, s}]
In[2]:= y[10]
Out[2]= {{tail,tail,head,tail,head,tail,head,head,tail,tail},6}
Note, I would implement this simulation as:
n = 100;
g = RandomInteger[1,n];
Total[g]
Assuming I needed I needed a set of heads/tails to do further
calculations on. If I only needed the sum, i.e., counts for
heads, I would code the simulation as
RandomInteger[BinomialDistribution[n,1/2]]
=46or small values of n, there won't be much difference between
these. But for very large values of n, I believe you will find
RandomInteger to be faster than RandomChoice.
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web