抛硬币问题 [英] coin toss problem

查看:68
本文介绍了抛硬币问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个抛硬币计划,并且无法弄清楚为什么它不会给出准确的结果...

欢呼,

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_pa​​ssed)

{

int i,x,row_h;

row_h = 0;


seed =(1 + seed_pa​​ssed);

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;


其他

A [i] = T;

}


A [i] = SENTINEL;


i = 0;


while( A [i]!= SENTINEL)

{

而(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(\ n输入硬币投掷次数:);

scanf("%d",& limit);


printf(\ nEn输入行中的头数:);

scanf("%d",& in_a_row);


if(limit< = 1000)

iterations = 100000;


for(i = 1; i< = iterations; i ++ )

{

one_run(种子);

}


(av_occur = 1.0 * count / iterations);


printf(\ n如果硬币被翻转%d次:\ n,限制);

printf(" ;平均没有。连续出现%d个头的百分比是%.5f"

,in_a_row,av_occur);

printf(" \ n(带有%d个测试)" ;,迭代);


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

* /

返回0;

}

解决方案

ce ********** @ hotmail.com 写道:


我有写了这个抛硬币计划,并且无法弄清楚为什么它没有给出准确的结果......
欢呼,


#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_pa​​ssed)
{
int i,x, row_h;
row_h = 0;

seed =(1 + seed_pa​​ssed);
srand(种子);

char A [lim它+ 1];




这个代码不应该编译,除非你有一个C99系统,我怀疑。 limit + 1不是常量,所以编译器不知道

要做什么。此外,您在可执行的

代码后定义存储。提高警告级别。你也可以在表达中使用

空白,几年前空白禁运被解除了。


-

" ;每次都是正确的男人不太可能做得很多。

- 弗朗西斯克里克,共同发现DNA

没有更多比行动中的愚蠢更令人惊讶。

- Thomas Matthews


CBFalconer写道:

ce**********@hotmail.com 写道:
< blockquote class =post_quotes>我写了这个抛硬币程序,不知道为什么它没有给出准确的结果......
欢呼,
Ben

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

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

void one_run(int seed_pa​​ssed)
{
int i,x,row_h;
row_h = 0;

seed =(1 + seed_pa​​ssed);
srand(种子);

char A [limit + 1];



这个代码不应该编译,除非你有一个C99系统,我怀疑。




为什么?使用我的任何一个编译器编译好。


-

Ian Collins。


< ce**********@hotmail.com>写道:

我写了这个抛硬币计划,并且无法弄清楚为什么它没有给出准确的结果...


我不知道你的问题是什么,但无论如何我都会做一些观察。

首先,摆脱全局变量。没有必要在这个简单易懂的程序中使用

到全局变量。


是否有任何理由不使用枚举而不是#defines ?它们更合适,可能会做你想要的。


不要使用哨兵。当有

这是一个很好的理由时,你可以使用哨兵。这里没有充分的理由。


将程序简化为其本质。您似乎能够处理命令

行,一组默认数据或用户提供的数据。这只是现在混淆的事情。选一个。我会使用内置数据进行初步测试。


注意in_a_row的定义。这是否意味着*确切* n在

行中?代码说没有。要确定你的代码必须查看

下一个字符 - 那个打破序列的字符 - 并且它不会这样做

。对我来说,计算是最明智的事情。例如,

如果in_a_row为3,则以下序列的头数被计算为:

001111111100?


我试过在角落的情况下,连续= 1并且连续= 0并且变得奇怪

结果。


你可能会拒绝接受这样的问题,例如n = 1,在最终版本中

你的代码。


关键数字是数。如果您仍有问题,请添加代码以打印

计数的原始值。


还有一些嵌入式注释。

#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_pa​​ssed)
{
int i,x,row_h;
row_h = 0;


不要将定义与初始值分开。


IOW

int row_h = 0;

seed =(1 + seed_pa​​ssed);
srand(种子);

char A [limit + 1];

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


太复杂了。


请注意,如果绘制的数字小于1/2 RAND_MAX,则已划分

分配到两个几乎相等的部分。

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


A [i] = T;
}
A [i] = SENTINEL;

i = 0;

while(A [我]!= 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)掷硬币数量:);
scanf(%d,& limit);

printf(\ n \\ n \\ n \\ n \\ n \\ n \\ n> :");
scanf("%d",& in_a_row);

if(limit< = 1000)
iterations = 100000;

for(i = 1; i <= i terations; i ++)
{
one_run(种子);
}

(av_occur = 1.0 * count / iterations);


那些parens是为了什么?只是一种空闲的问题。

printf(\ n如果硬币被翻转%d次:\ n,限制);
printf(" Average no.of连续出现的%d个百分比是%.5f
,in_a_row,av_occur);
printf(" \ n(带有%d个测试)",迭代);

/ * printf(" \\\
count is%d",count);
* /
返回0;
}



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;
}

解决方案

ce**********@hotmail.com wrote:


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];



This code should not compile unless you have a C99 system, which I
doubt. limit+1 is not a constant, so the compiler has no idea what
to do. In addition, you are defining storage after executable
code. Turn up your warning levels. Also you are allowed to use
blanks in expressions, the blank embargo was lifted some years ago.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


CBFalconer wrote:

ce**********@hotmail.com wrote:

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];


This code should not compile unless you have a C99 system, which I
doubt.



Why? Compiles fine with either compiler on mine.

--
Ian Collins.


<ce**********@hotmail.com> wrote:

I''ve written this coin toss program, and can''t figure out why it isn''t
giving accurate results...
I don''t know what your problem is but I will make some observations anyway.
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.

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

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

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.

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 ?

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.

The crucial number is count. If you still have problems, add code to print
the raw value of count.

There are also a few embedded comments.
#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;
Don''t separate the definition from the initial value.

IOW
int 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));
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.
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);
What are those parens for? Just a kind of idle question.
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;
}



这篇关于抛硬币问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆