为什么这不会产生随机数!? [英] Why will this not generate a random number!?

查看:86
本文介绍了为什么这不会产生随机数!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯了。我在学校已经完成了基础C,但是我的b
教育主要基于面向对象的设计理论,其中Java

是我们的工具。出于某种原因,在帮助朋友的同时使用C $ / $
编程实验室。我不能为我的生活生成一个随机数。

我不知道出了什么问题。请帮忙。


#include< stdio.h>

#include< stdlib.h>

#include< ; math.h>


#define RAND_MAX 32768

int main(无效)

{

浮动b;


b =((浮动)rand()/ RAND_MAX);

printf("随机数:%f" ,b);


系统(PAUSE);

返回0;

}

This has been driving me crazy. I''ve done basic C in school, but my
education is mainly based on object oriented design theory where Java
is our tool. For some reason, while helping a friend with a C
Programming lab. I cannot for the life of me generate a random number.
i don''t know what is wrong. please help.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define RAND_MAX 32768

int main(void)
{
float b;

b = ((float)rand()/RAND_MAX);
printf("Random Number: %f", b);

system("PAUSE");
return 0;
}

推荐答案

John Cassidy写道:
John Cassidy wrote:
这让我发疯了。我在学校完成了基础C课程,但我的教育主要基于面向对象的设计理论,其中Java是我们的工具。出于某种原因,在帮助朋友使用C
编程实验室时。我不能为我的生活生成一个随机数。
我不知道出了什么问题。请帮忙。


rand()将生成特定的随机,以及我对以下代码的补充。

#include< stdio.h>
#include< stdlib.h>
#include< math.h>

#define RAND_MAX 32768

int main(void)
{
浮b;


//函数srand()用于播种由
// rand()生成的随机序列。对于任何给定的种子,rand()将一遍又一遍地生成特定的随机

//序列。

srand(time(NULL));

b =((float)rand()/ RAND_MAX);

printf(" Random Number:%f",b);

system( PAUSE;;
返回0;
}
This has been driving me crazy. I''ve done basic C in school, but my
education is mainly based on object oriented design theory where Java
is our tool. For some reason, while helping a friend with a C
Programming lab. I cannot for the life of me generate a random number.
i don''t know what is wrong. please help.
rand() will generate spesific "random", se my addition to the code below.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define RAND_MAX 32768

int main(void)
{
float b;
// The function srand() is used to seed the random sequence generated by
// rand(). For any given seed, rand() will generate a specific "random"
// sequence over and over again.
srand(time(NULL));
b = ((float)rand()/RAND_MAX);
printf("Random Number: %f", b);

system("PAUSE");
return 0;
}




如果你在UNIX系统上,你可能要考虑使用/ dev / urandom

随机生成。


-

问候

Hans-Christian Egtvedt



If you''re on a UNIX system you might want to consider using /dev/urandom
to generate random.

--
Regards
Hans-Christian Egtvedt


jo ********** @ gmail.com (John Cassidy)写道:
jo**********@gmail.com (John Cassidy) writes:
这让我发疯了。我在学校完成了基础C课程,但我的教育主要基于面向对象的设计理论,其中Java是我们的工具。出于某种原因,在帮助朋友使用C
编程实验室时。我不能为我的生活生成一个随机数。
我不知道出了什么问题。请帮助。

#include< stdio.h>
#include< stdlib.h>
#include< math.h>
#define RAND_MAX 32768
int main(void)
{
浮动b;

b =((float)rand()/ RAND_MAX) ;

printf(随机数:%f,b);

系统(暂停);
返回0;
}
This has been driving me crazy. I''ve done basic C in school, but my
education is mainly based on object oriented design theory where Java
is our tool. For some reason, while helping a friend with a C
Programming lab. I cannot for the life of me generate a random number.
i don''t know what is wrong. please help.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define RAND_MAX 32768

int main(void)
{
float b;

b = ((float)rand()/RAND_MAX);
printf("Random Number: %f", b);

system("PAUSE");
return 0;
}




感谢您发布完整的程序(太多人发布了

片段或伪代码)。但是关于你实际看到的行为是什么行为的更多细节会有所帮助。你说它不会产生一个随机数的b $ b;它是做什么的?


你不需要< math.h> ;; rand()在< stdlib.h中声明>


不要自己定义RAND_MAX,它是在< stdlib.h>中定义的。


你的输出不会以换行结束;在系统上,这可能会导致你的输出完全不显示

。如果

系统没有暂停命令,系统(暂停)将无法工作。


您的系统应该有文档rand()和srand()

函数。找到它并阅读它。


我认为你的实际问题可能是通过

C FAQ的条目13.17来解答的,< http:// www。 eskimo.com/~scs/C-faq/q13.17.html> ;.问题

13.15到13.20处理随机数。阅读整个部分。

阅读整个常见问题解答。


如果你在没有先调用srand()的情况下调用rand(),它就相当于

在调用srand(1)之后调用rand()。这总是给你相同的

数字序列。在我刚试过的一个系统上,第一次打电话给

rand()给了我0.


-

Keith Thompson (The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Thank you for posting a complete program (too many people post
fragments or pseudo-code). But a little more detail on what behavior
you''re actually seeing would be helpful. You say it doesn''t generate
a random number; what does it do?

You don''t need <math.h>; rand() is declared in <stdlib.h>

Don''t define RAND_MAX yourself, it''s defined for you in <stdlib.h>.

Your output doesn''t end in a newline; on systems, this might cause
your output not to appear at all. system("PAUSE") won''t work if the
system doesn''t have a PAUSE command.

Your system should have documentation on the rand() and srand()
functions. Find it and read it.

I think your actual problem is probably answered by entry 13.17 of the
C FAQ, <http://www.eskimo.com/~scs/C-faq/q13.17.html>. Questions
13.15 through 13.20 deal with random numbers. Read the whole section.
Read the whole FAQ.

If you call rand() without first calling srand(), it''s equivalent to
calling rand() after calling srand(1). This always gives you the same
sequence of numbers. On one system I just tried, the first call to
rand() gave me 0.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


John Cassidy写道:
John Cassidy wrote:
这让我发疯。我在学校完成了基础C课程,但我的教育主要基于面向对象的设计理论,其中Java是我们的工具。出于某种原因,在帮助朋友使用C
编程实验室时。我不能为我的生活生成一个随机数。
我不知道出了什么问题。请帮忙。


在我们开始这里之前,请注意这完全包含在

常见问题解答中,并且通常在新闻组中接受检查

发布前的常见问题解答。在你大喊之前,但我不知道有一个

常见问题,另请注意,在发布之前潜伏在

新闻组中通常是公认的做法;这里很多人都有链接到他们的sig中的常见问题解答,如果你潜伏在预期的期间,

你会看到其中一个定期发布的帖子常见问题解答本身。

#include< stdio.h>
#include< stdlib.h>
#include< math.h>

#define RAND_MAX 32768


不,你不这样做。 RAND_MAX是一个由你的实现定义的宏。

int main(void)
{
浮动b;


你应该,除非你每次运行

程序时想要相同的序列,否则在使用

之前使用srand()为随机数生成器播种一次。有关详细信息,请参阅常见问题解答。

b =((float)rand()/ RAND_MAX);


好​​的,所以你希望b在[0,1]范围内。但是,一般情况下,你真的想要b在[0,1)范围内的b:

b = rand()/(RAND_MAX + 1。);

printf(随机数:%f,b);

system(PAUSE);


你不依赖于主机系统有一个名为

" PAUSE"的命令。

getchar() ;

会完成同样的事情,不是吗?返回0;
}
This has been driving me crazy. I''ve done basic C in school, but my
education is mainly based on object oriented design theory where Java
is our tool. For some reason, while helping a friend with a C
Programming lab. I cannot for the life of me generate a random number.
i don''t know what is wrong. please help.
Before we get started here, please note that this is fully covered in
the FAQ, and it is normally accepted practice in newsgroups to check the
FAQ before posting. Before you shout, "But I didn''t know there was a
FAQ," note also that it is normally accepted practice to lurk in
newsgroups before posting; many people here have links to the FAQ in
their sigs and, if you had lurked for anything like the expected period,
you would have seen one of the periodic postings of the FAQ itself.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define RAND_MAX 32768
No, you don''t do this. RAND_MAX is a macro defined by your implementation.

int main(void)
{
float b;

You ought to, unless you want the same sequence everytime you run the
program, seed the random number generator once with srand() before using
it. See the FAQ for details.
b = ((float)rand()/RAND_MAX);
OK, so you want b in the range [0,1]. Generally, however, you really
want b in the range [0,1):
b = rand()/(RAND_MAX+1.);
printf("Random Number: %f", b);

system("PAUSE");
You are getter off not relying on the host system having a command named
"PAUSE".
getchar();
would accomplish the same thing, wouldn''t it? return 0;
}



这篇关于为什么这不会产生随机数!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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