我的错误在哪里? [英] Where is my mistake?

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

问题描述

我是自学C程序的学生。

希望有人可以帮我解决这个问题。

这个程序使用调用函数在用户定义的范围内生成随机数

我使用了调用函数 GenRndNum" ;.范围是2和10.

问题是我获得了2次调用产生的相同的2个随机数。

我应该得到2个不同的随机数。有人可以指出我的错误吗?

谢谢

Khoon。

/ *在用户定义的范围内生成随机数。* /


#include< stdio.h>

#include< stdlib.h>

#include< time.h> ;

int GenRndNum(int x,int y);


int main(无效)


{

int MinRange;

int MaxRange;


int RndNum1;

int RndNum2;


printf(请输入两个范围的最小值和最大值);

printf(" \\\
random numbers>" );

scanf("%d%d",& MinRange,& MaxRange);

RndNum1 = GenRndNum(MinRange,MaxRange);

printf(" RndNum1 =%d \ n",RndNum1);


RndNum2 = GenRndNum(MinRange,MaxRange);

printf (RndNum2 =%d \ n,RndNum2);


返回0;

}


int G. enRndNum(int x,int y)


{int z;

srand(time(NULL));

printf( x =%d,y =%d \ n,x,y);


z = rand()%((y + 1) - x)+ x;

printf(" z =%d \ n",z);


return(z);

}

/ * -----------------------

输出结果:


请输入两个

随机数范围的最小值和最大值> 2 10

x = 2,y = 10

z = 5

RndNum1 = 5

x = 2,y = 10

z = 5

RndNum2 = 5

按任意键继续_

* /

I am self study C program student.
Hope someone can help me with this problem.
This program generates random numbers over a user defined range using call function
I used the call function " GenRndNum". The range is 2 and 10.
The problem is that I get the same 2 random numbers generated over 2 calls.
I should get 2 different random numbers. Can someone please point out my mistake?
Thanks
Khoon.
/* Generation of Random Numbers over a User Defined Range.*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int GenRndNum (int x, int y);

int main (void)

{
int MinRange;
int MaxRange;

int RndNum1;
int RndNum2;

printf ("Please key in the Minimum and Maximum Value for the Range of the two ");
printf ("\nrandom numbers> ");
scanf ( "%d %d", &MinRange, &MaxRange);
RndNum1= GenRndNum (MinRange, MaxRange);
printf ("RndNum1 = %d\n",RndNum1);

RndNum2= GenRndNum (MinRange, MaxRange);
printf ("RndNum2 = %d\n",RndNum2);

return 0;
}

int GenRndNum (int x, int y )

{ int z;
srand (time(NULL));
printf ("x = %d, y = %d\n",x,y);

z = rand() % ((y+1 ) - x ) + x;
printf ("z = %d\n",z);

return (z);
}
/*-----------------------
Result of Output:

Please key in the Minimum and Maximum Value for the Range of the two
random numbers> 2 10
x = 2, y = 10
z = 5
RndNum1 = 5
x = 2, y = 10
z = 5
RndNum2 = 5
Press any key to continue_
*/

推荐答案

Red Dragon写道:
Red Dragon wrote:
我是自学C程序的学生。
希望有人可以帮我解决这个问题。
I am self study C program student.
Hope someone can help me with this problem.



< snip>


请不要将HTML发布到Usenet。很多客户都无法处理这个问题,即使是那些在回复时也无法将其转换为文本的人。


您正在使用Outlook Express。要更改格式,请选择工具 - >

选项,单击发送选项卡,并确保单选按钮Plain

Text正在进行新闻发送格式。


其余的消息被忽略,抱歉。


S.


<snip>

Please do not post HTML to Usenet. Many clients can''t handle this and
even those who can have trouble converting it to text when replying.

You''re using Outlook Express. To change the format, choose Tools ->
Options, click on the Send tab, and make sure the radio button "Plain
Text" is on for "News Sending Format".

Rest of the message ignored, sorry.

S.


Red Dragon写道:
Red Dragon wrote:
[...]
问题是我获得了2次调用产生的相同的2个随机数。
我应该得到2个不同的随机数。有人可以指出我的错误吗?
[...]
int GenRndNum(int x,int y)

{int z;
srand(time(空值));


这是它。请参阅comp.lang.c

常见问题(FAQ)列表中的问题13.17

http://www.eskimo.com/~scs/C-faq/top.html


事实上,见所有问题13.15到13.20;还有

与你正在做的事情有关的其他一些问题,

FAQ可以帮助你解决它们。

printf(" ; x =%d,y =%d \ n",x,y);

z = rand()%((y + 1) - x)+ x;
printf(" z =%d \ n",z);

return(z);
}
[...]
The problem is that I get the same 2 random numbers generated over 2 calls.
I should get 2 different random numbers. Can someone please point out my mistake?
[...]
int GenRndNum (int x, int y )

{ int z;
srand (time(NULL));
Here it is. See Question 13.17 in the comp.lang.c
Frequently Asked Questions (FAQ) list at

http://www.eskimo.com/~scs/C-faq/top.html

In fact, see all of Questions 13.15 through 13.20; there
are a few other problems with what you''re doing, and the
FAQ can help you solve them.
printf ("x = %d, y = %d\n",x,y);

z = rand() % ((y+1 ) - x ) + x;
printf ("z = %d\n",z);

return (z);
}




-

Eric Sosman
es ***** @ acm-dot- org.inva lid



--
Eric Sosman
es*****@acm-dot-org.invalid


你总是使用相同的srand值。试试这个:


long tm;

time(& tm);

srand(tm);

you are using always the same srand value. try this:

long tm;
time(&tm);
srand(tm);


这篇关于我的错误在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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