Re: coin toss problem



On 2006-06-18, Ben <celerysoup16@xxxxxxxxxxx> wrote:
Hi,

I don't know what your problem is but I will make some observations anyway.

My initial problem was that I had read in a maths book that the average
no. of 6 heads in a row in a sequence of 200 tosses was at least one...
my program kept providing 0.77
I have since realised that they must have meant "at least 6"...

First of all, get rid of the global variables. There is no need to resort
to global variables in a program this small and simple.

I only started learning programming a few months ago- so I'm not sure
what the problem is with global variables? (And what's an alternative?)

Globals are very difficult to maintain; it is impossible to tell what
functions modify the variables, and where. Much better to pass each variable
to each function.

Is there any reason not to use enum instead of the #defines? They are more
appropriate and would probably do what you want.

The #defines seem to do what I want - enums might be more appropriate,
but I haven't yet learnt what they are :)

enums are lists of variables:

enum MyList { itemOne, itemTwo, itemThree };

results in itemOne being 0, itemTwo being 1, and itemThree being 2. You can
also add in definitions:

enum MyList { itemOne, itemTwo = 9, itemThree };

will set itemTwo to 9.

Don't use the sentinel. Sentinels are something you resort to when there is
a good reason. There is no good reason here.

The sentinel seemed easy enough to use - what potential problems are
there with using them? Why are they only a last resort?

Sentinel values are difficult to read and maintain; there are far more elgant
solutions to most problems.

Reduce the program to its essence. You seem to be able to handle command
line, a default set of data, or user supplied data. This is just
obfuscating things right now. Choose one. I would use built in data for
initial testing.

The command line data is just for the seed, so I can manually assess
the degree of probability accuracy ... the default data is probably
unnecessary.

Ensure that your program will work with default values /first/, and then add
functionality for command lines. There's a reason that people are paid large
sums of money for programming: it isn't a one-step easy-to-do process.

Pay attention to the definition of in_a_row. Does it mean *exactly* n in a
row? The code says no. To determine that your code would have to look at
the next character - the one that breaks the sequence- , and it doesn't do
that. To me, exactly is the most sensible thing to compute. For example,
if in_a_row is 3 what does the following sequence of heads get counted as:
001111111100 ?

It does compute 'exactly x in a row' ... which is what I intended...
(it looks at the character that breaks the sequence when 'i' is
incremented - which breaks the while loop)

while(A[i]==H)
{
i++;
row_h++;
}

if(row_h==in_a_row)
count++;

row_h=0;

Don't use tabs on Usenet; use either 2 or 4 spaces for indentation. I've
heard of people using 3 spaces as well. Anything higher or lower is difficult
to read.

I tried the corner cases, in a row = 1 and in a row = 0 and got strange
results.
You might refuse to accept such questions as n = 1 in the final version of
your code.

I'd like to be able to test for n=1 ... so I'll keep trying :)
If n=0, I could just assign the number of tosses to n, and test for
that instead.


if (n == 1)
{
/* Do stuff here */
}

Tests aren't hard in programming languages.

x=(int)(2.0*rand()/(1.0+RAND_MAX));

Too complicated.

Note that if the number drawn is less than 1/2 RAND_MAX, you have divided
the distribution into two almost equal parts.

My uni textbook says "If you need your program to be portable, and
require the values to be uncorelated even when compiled with the
original rand and srand functions, you should extract the desired value
from high-order bits rather than low-order ones..."

I can't imagine how you would make a program "portable" in that you will always
get reasonably random values. If I was in a malicious mood, I could develop a
system where rand() would always return an integer between 0.9 and 1.1.

(av_occur=1.0*count/iterations);

What are those parens for? Just a kind of idle question.

I'm a newbie ... by habbit I tend to stick in as many extra parens as
possible ;)
(I realise now they are reduntant).

"habit" is how it is spelled. In general, you should avoid unnecessary
parentheses except in long or esoteric expressions.

Ex. x = (y << 3) + b;
x = (a + b) + (c - b) * (r + a) / ((t == y) ? m : n);

I hope that you never type that second one. :-)

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
.



Relevant Pages

  • Re: Linfords Fallacy
    ... He is comparing apples to apples and your comparing oranges ... He is talking about sequences of all heads or all tails verses sequences ... sequence to any other sequence. ... probability and the other has a very low probability. ...
    (sci.math)
  • Re: Linfords Fallacy
    ... "While it is true that flipping out any given sequence of heads/tails is ... it is not true that the probability of having all heads ... He's singling out a string of all heads and ignoring the ... with identical electrons as any other. ...
    (sci.math)
  • Re: Linfords Fallacy
    ... it is not true that the probability of having all ... heads is the same as the probability as having any mixture of heads and ... or tails than there are series of coin flips with only heads. ... It is true that the probabiliy of any sequence is the same ...
    (sci.math)
  • Re: Sean Pitmans (and other creationists) selective credulity
    ... For some reason, he wants to be convinced that detailed convergence in ... This claims that the protein relaxin is identical in sequence between a ... But Christian Schwabe, the author pushing this factoid, wants to believe ... And Sean wants to believe the result for his own reasons, ...
    (talk.origins)
  • Re: Sean Pitmans (and other creationists) selective credulity
    ... For some reason, he wants to be convinced that detailed convergence in ... This claims that the protein relaxin is identical in sequence between a ... But Christian Schwabe, the author pushing this factoid, wants to believe ... And Sean wants to believe the result for his own reasons, ...
    (talk.origins)

Loading