兰德和桑德 [英] rand and srand

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

问题描述

我对这个感到难过。我尝试了两种方法,而且只是没有什么东西,而且b $ b似乎是正确的。我会试试我的新系列。


#include< stdio.h>

#include< stdlib.h>


main(){

srand(2000); / * seed unsigned * /

printf("%u",rand());

}


现在我得到一个比2000大得多的数字。此外,当我也不时用

srand尝试RAND_MAX时。


主要的我总是被告知int是默认类型并且不需要声明
。 Main()一直有效。我希望这段代码更具可读性。


Bill

解决方案

Bill Cunningham写道:


< about main>


C99标准想要和int。



或者与int兼容的类型。


[...]所以真的需要使用srand()?



是的。如果你想让你的下一个伪随机序列不同而且不重复




On Sun,2008年3月9日14 :57:38 -0600,Falcon Kirtaran

< fa ******** @ iridiumlinux.orgwrote in comp.lang.c:


Bill Cunningham写道:


我对此感到难过。我尝试了两种方法,而且只是没有什么东西,而且b $ b似乎是正确的。我会试试我的新系列。


#include< stdio.h>

#include< stdlib.h>


main(){

srand(2000); / * seed unsigned * /

printf("%u",rand());

}


现在我得到一个比2000大得多的数字。此外,当我也不时用

srand尝试RAND_MAX时。


主要的我总是被告知int是默认类型并且不需要声明
。 Main()一直有效。我希望这段代码更具可读性。


比尔



[snip]
< blockquote class =post_quotes>
至于你的main(),我实际上并不知道这样的代码是合法的C.



无论在哪里你明白了吗?在1999年C语言标准更新之前,该代码在C up

中完全合法。


我的猜测是它实际上是无效的main(),因为你永远不会返回



现在你变得很傻了。在C99之前,在合法的每个地方定义或声明没有显式类型的东西,它是隐式输入为int的

。永远无效。


main()

$ C $ b ....在C99之前,完全相同:


int main()


....并且在C99及更高版本下是非法的,因为所有声明者

必须明确声明类型。


a值,因此您的程序的退出状态很可能是未定义的
。将它声明为int,

并在结尾返回0(除非程序失败),这是一个好主意,特别是在UNIX中。



这在任何版本的C标准下都是正确的。如果一个程序

落到最后没有返回值的main(),返回到环境的退出

状态是不确定的。


-

杰克Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http:// www。 club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


Jack Klein< ja ******* @ spamcop.netwrites:


On Sun,2008年3月9日14:57:38 -0600,Falcon Kirtaran

< fa ***在comp.lang.c中的@@iridiumlinux.orgwrote:



[ ...]


>来自它的值,因此程序的退出状态很可能是未定义的。将它声明为int,
并在结尾返回0(除非程序失败),这是一个好主意,特别是在UNIX中。



这在任何版本的C标准下都是正确的。如果一个程序

落到最后 main()没有返回值,返回到环境的退出

状态是未定义的。



不,C99添加了一个特殊情况规则,从main()的结尾掉落

执行隐式返回0;英寸;见C99 5.1.2.2.3。


-

Keith Thompson(The_Other_Keith)< ks *** @ mib.org>

诺基亚

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

- Antony Jay和Jonathan Lynn,是部长


I am stumped on this one. I tried two methods and something just doesn''t
seem right. I''ll try my new syle.

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

main() {
srand(2000); /*seed unsigned */
printf("%u",rand());
}

Now I get a number much larger than 2000. Also when I also try RAND_MAX with
srand from time to time.

With main I was always told int was the default type and didn''t need to be
declared. Main() has always worked. I hope this code is much more readable.

Bill

解决方案

Bill Cunningham wrote:

<about main>

The C99 standard wants and int.

Or a type compatible with int.

[...] So is there really a need to use srand ( ) ?

Yes. If you want your next pseudo-random sequence to be different and
not repeat.


On Sun, 09 Mar 2008 14:57:38 -0600, Falcon Kirtaran
<fa********@iridiumlinux.orgwrote in comp.lang.c:

Bill Cunningham wrote:

I am stumped on this one. I tried two methods and something just doesn''t
seem right. I''ll try my new syle.

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

main() {
srand(2000); /*seed unsigned */
printf("%u",rand());
}

Now I get a number much larger than 2000. Also when I also try RAND_MAX with
srand from time to time.

With main I was always told int was the default type and didn''t need to be
declared. Main() has always worked. I hope this code is much more readable.

Bill

[snip]

As for your main(), I wasn''t actually aware that such code was legal C.

Wherever did you get that idea? That code was perfectly legal in C up
until the 1999 update to the C language standard.

My guess is that it is actually void main(), because you never return

Now you''re getting silly. Prior to C99, everyplace where it was legal
to define or declare something without an explicit type, it was
implicitly typed as int. Never as void.

main()

....prior to C99, was exactly identical to:

int main()

....and is illegal under C99 and later versions, as all declarators
must explicitly declare a type.

a value from it, so the exit status of your program is most likely
undefined. It is a good idea, particularly in UNIX, to declare it int,
and return 0 at the end (unless the program failed).

This is correct under any version of the C standard. If a program
"falls off the end" of main() without returning a value, the exit
status returned to the environment is undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


Jack Klein <ja*******@spamcop.netwrites:

On Sun, 09 Mar 2008 14:57:38 -0600, Falcon Kirtaran
<fa********@iridiumlinux.orgwrote in comp.lang.c:

[...]

>a value from it, so the exit status of your program is most likely
undefined. It is a good idea, particularly in UNIX, to declare it int,
and return 0 at the end (unless the program failed).


This is correct under any version of the C standard. If a program
"falls off the end" of main() without returning a value, the exit
status returned to the environment is undefined.

No, C99 added a special-case rule that falling off the end of main()
does an implicit "return 0;"; see C99 5.1.2.2.3.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于兰德和桑德的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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