473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

coin toss problem

I've written this coin toss program, and can't figure out why it isn't
giving accurate results...
cheers,
Ben

#include <stdlib.h>
#include <stdio.h>
#define H 1
#define T 0
#define SENTINEL -1

int count=0, seed=3645, limit=200, in_a_row=6, iterations=100;

void one_run(int seed_passed)
{
int i, x, row_h;
row_h = 0;

seed=(1+seed_passed);
srand(seed);

char A[limit+1];

for(i=0;i<limit;i++)
{
x=(int)(2.0*rand()/(1.0+RAND_MAX));

if(x==H)
A[i]=H;

else
A[i]=T;
}

A[i]=SENTINEL;

i=0;

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

if(row_h==in_a_row)
count++;

row_h=0;

if(A[i]!=SENTINEL)
i++;
}
}

int main(int argc, char **argv)
{
int i;
double av_occur;

if (argc==2)
seed=atoi(argv[1]);

printf("\nEnter number of coin tosses: ");
scanf("%d", &limit);

printf("\nEnter number of heads to be found in a row: ");
scanf("%d", &in_a_row);

if(limit<=1000)
iterations=100000;

for(i=1;i<=iterations;i++)
{
one_run(seed);
}

(av_occur=1.0*count/iterations);

printf("\nIf a coin is flipped %d times:\n", limit);
printf("Average no. of occurances of %d heads in a row is %.5f"
, in_a_row, av_occur);
printf("\n(with %d tests)", iterations);

/* printf("\ncount is %d", count);
*/
return 0;
}

Jun 17 '06
52 5287
Old Wolf said:
Whether or not constraint violations are UB is a bit of an
academic matter (half the NG seems to think they are and
half think they aren't).


I don't think it matters either way, provided they continue to be regarded
as show-stoppers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 19 '06 #51
osmium said:
"osmium" writes:
Here's another cut at it.


I see the copy I posted is where I was fooling around trying to see how
many
trials it took to get a result of 1.0 It is in the vicinity of 128
trials.
Change the 128 to 200 to customize this to your question. Also note this
was compiled on a compiler that accepts some C99 syntax, E.g., \\
comments.


\\ comments? :-)

Incidentally, I am wary of a comment syntax that breaks when the text is
re-flowed, and which in any case constitutes a syntax error in a great many
extant implementations.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 19 '06 #52
On Mon, 19 Jun 2006 06:09:14 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalid> wrote:
osmium said:
was compiled on a compiler that accepts some C99 syntax, E.g., \\
comments.


\\ comments? :-)


for writing C in Arabic, of course.

gd&r
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 19 '06 #53

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Monteleone | last post by:
Greetings, I posted a few days back and didnt get much of a response, so I figured I would post again with more detail. I am running gnome under fedora core 4. I want a kid to be able to drop a...
12
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
3
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total...
0
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times....
5
by: sallyk57 | last post by:
I have to make a program that would ask a user for their guess (heads or tails) and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do...
6
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main...
8
by: PAK11 | last post by:
This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result...
0
Chuncatty
by: Chuncatty | last post by:
How much does a Draped Bust coin fabricate cost? (Sorry for my Eng) I ask a beau to see if it's genuine he said it is, down repay a collage professor i met said so. It's in virtue word,...
18
by: Chinde | last post by:
Hi I'm using AS2 to produce a simple platform game. So far so good I would like the character to collect coins or whatever. so what I have done is create a coin movie clip, I've run a hittest on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.