MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Coin Toss Sim

  • To: mathgroup at smc.vnet.net
  • Subject: [mg122586] Re: Coin Toss Sim
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Wed, 2 Nov 2011 06:22:17 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <j89v28$8e$1@smc.vnet.net>

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




  • Prev by Date: Question: 2 z-Axis scales in Plot3D?
  • Next by Date: Re: Problems with DistributionFitTest
  • Previous by thread: Re: Question: 2 z-Axis scales in Plot3D?
  • Next by thread: Re: Putting Points and Lines into RegionPlot3D