Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5883 > unrolled thread
| Started by | pooleman133 <poolebc221@gmail.com> |
|---|---|
| First post | 2011-10-26 21:45 +0000 |
| Last post | 2011-11-02 11:27 +0000 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Coin Toss Sim pooleman133 <poolebc221@gmail.com> - 2011-10-26 21:45 +0000
Re: Coin Toss Sim Rui <rui.rojo@gmail.com> - 2011-10-27 10:40 +0000
Re: Coin Toss Sim "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-10-28 09:33 +0000
Re: Coin Toss Sim "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-10-29 11:21 +0000
Re: Coin Toss Sim Dan Dubin <ddubin@ucsd.edu> - 2011-10-29 11:15 +0000
Re: Coin Toss Sim DrMajorBob <btreat1@austin.rr.com> - 2011-10-29 11:20 +0000
Re: Coin Toss Sim "Chris Degnen" <degnen@cwgsy.net> - 2011-11-01 05:10 +0000
Re: Coin Toss Sim David Bailey <dave@removedbailey.co.uk> - 2011-11-02 11:27 +0000
| From | pooleman133 <poolebc221@gmail.com> |
|---|---|
| Date | 2011-10-26 21:45 +0000 |
| Subject | Coin Toss Sim |
| Message-ID | <j89v28$8e$1@smc.vnet.net> |
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}
]
[toc] | [next] | [standalone]
| From | Rui <rui.rojo@gmail.com> |
|---|---|
| Date | 2011-10-27 10:40 +0000 |
| Message-ID | <j8bce4$8rp$1@smc.vnet.net> |
| In reply to | #5883 |
Every place you write g it runs the RandomChoice code again. So, the values of g it uses when evaluating your s in {g, s} is not the same as the one it uses in the g in {g, s}.
You want the list for the same realization. I would write something like
y[n_]:=With[
{g=RandomChoice[{"head", "tail"}, n]},
{g, Count[g, "head"]}
]
[toc] | [prev] | [next] | [standalone]
| From | "Dr. Wolfgang Hintze" <weh@snafu.de> |
|---|---|
| Date | 2011-10-28 09:33 +0000 |
| Message-ID | <j8dstg$k5b$1@smc.vnet.net> |
| In reply to | #5883 |
"pooleman133" <poolebc221@gmail.com> schrieb im Newsbeitrag
news:j89v28$8e$1@smc.vnet.net...
>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}]
y[10]
{{"tail", "head", "head", "head", "tail", "tail",
"tail", "head", "head", "tail"}, 3}
-- Wolfgang
[toc] | [prev] | [next] | [standalone]
| From | "Dr. Wolfgang Hintze" <weh@snafu.de> |
|---|---|
| Date | 2011-10-29 11:21 +0000 |
| Message-ID | <j8gnj2$5fb$1@smc.vnet.net> |
| In reply to | #5952 |
Sorry, my solution is nonsense (five times "head" in the list, only 3
times in count).
The point is indeed the delayed evaluation for g, as many of you have
pointed out.
--- Wolfgang
"Dr. Wolfgang Hintze" <weh@snafu.de> schrieb im Newsbeitrag
news:j8dstg$k5b$1@smc.vnet.net...
>
> "pooleman133" <poolebc221@gmail.com> schrieb im Newsbeitrag
> news:j89v28$8e$1@smc.vnet.net...
>>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}]
> y[10]
>
> {{"tail", "head", "head", "head", "tail", "tail",
> "tail", "head", "head", "tail"}, 3}
> -- Wolfgang
>
>
[toc] | [prev] | [next] | [standalone]
| From | Dan Dubin <ddubin@ucsd.edu> |
|---|---|
| Date | 2011-10-29 11:15 +0000 |
| Message-ID | <j8gn8h$5cg$1@smc.vnet.net> |
| In reply to | #5883 |
>"pooleman133" <poolebc221@gmail.com> schrieb im Newsbeitrag
>news:j89v28$8e$1@smc.vnet.net...
>>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}]
>y[10]
>
>{{"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@ucsd.edu
[toc] | [prev] | [next] | [standalone]
| From | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| Date | 2011-10-29 11:20 +0000 |
| Message-ID | <j8gnh5$5en$1@smc.vnet.net> |
| In reply to | #5883 |
Wolfgang,
No, that doesn't work, as your output shows... a list with 5 heads but a
count of 3.
Bobby
On Fri, 28 Oct 2011 04:32:38 -0500, Dr. Wolfgang Hintze <weh@snafu.de>
wrote:
>
> "pooleman133" <poolebc221@gmail.com> schrieb im Newsbeitrag
> news:j89v28$8e$1@smc.vnet.net...
>> 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}]
> y[10]
>
> {{"tail", "head", "head", "head", "tail", "tail",
> "tail", "head", "head", "tail"}, 3}
> -- Wolfgang
>
>
--
DrMajorBob@yahoo.com
[toc] | [prev] | [next] | [standalone]
| From | "Chris Degnen" <degnen@cwgsy.net> |
|---|---|
| Date | 2011-11-01 05:10 +0000 |
| Message-ID | <j8nv0e$eip$1@smc.vnet.net> |
| In reply to | #5883 |
Simulating coin tosses was addressed on page 9 of Michael Morrison's
seminar download, Analyze, Visualize, Simulate, in conference Track
4: Applying Mathematica in Education
http://www.wolfram.com/events/virtual-conference-2011/
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}
> ]
>
[toc] | [prev] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2011-11-02 11:27 +0000 |
| Message-ID | <j8r9e4$3j1$1@smc.vnet.net> |
| In reply to | #5883 |
On 26/10/2011 22:45, 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}
> ]
>
From time to time, people post with questions like yours. The basic
problem is that they really need to learn a bit of Mathematica before
setting out to solve your own problems - guessing what to do doesn't work!
Rather than give you a piece of working code, I want to show you how to
learn how to solve a lot of your own problems.
1) Look up the functions If, While, Do in the help system. Try to
copy/paste some of the examples into a notebook. Run them and modify
them to explore what is going on.
2) Get used to trying the bits of your program on their own. E.g.
test out the RandomChoice call on its own.
3) Notice how you can add calls to Print inside code to explore what
it is doing. For example, in your code you might want to insert the line:
Print[g];
When you write a piece of code that is any way tricky, it is usually
best to add the Print calls to see what is going on, then take them out
later.
4) Until you really know what you are doing, always use Set (=) to
assign to variables, and SetDelayed (:=) to define functions.
David Bailey
http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web