问题描述
您好,
我正在尝试使用C获取一个随机数。我尝试过使用随机数(),
但我的程序一直在生成数字我输入的数字超出了范围。
有人有任何建议吗?
Kyle
-
comp.lang.c.moderated - 审核地址:
见,例如:
(以及常见问题解答中的其他附近条目)。
HTH,
- g
-
Artie Gold - 德克萨斯州奥斯汀
使用模数运算符(%)来表示数字的范围。
苏珊
-
comp.lang.c.moderated - 审核地址:
在线乘法你的程序中有76个应该是一个部门。
你拼写错误的onomatopoeia在第103行的评论中。
我当然只是在猜测,因为你没有发布任何
会让我们想到的东西实际问题可能是什么。
标准C中没有函数叫做random()。标准
定义了一个rand()函数(不幸的是,它的实现通常不是很好),所以使用你的系统的random()函数可能
给你更好的结果。我猜你已经阅读了
random()函数的文档。它可能返回范围很广的数字;
你需要做一些事情来将它们扩展到你想要的范围。
-
Keith Thompson(The_Other_Keith)< http://www.ghoti。 net / ~kst>
圣地亚哥超级计算机中心< *> < http://www.sdsc.edu/~kst>
Schroedinger做莎士比亚:要*和*不要
-
comp.lang.c.moderated - 审核地址:
Hello,
I''m trying to get a random number using C. I''ve tried using random(),
but my program keeps producing numbers that are way out of range from
the numbers I put in.
Anyone got any suggestions?
Kyle
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
See, for example:
http://www.eskimo.com/~scs/C-faq/q13.16.html
(as well as other nearby entries in the FAQ).
HTH,
--ag
--
Artie Gold -- Austin, Texas
Use the modulus operator (%) to bring the number in range.
Susan
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
The multiplication on line 76 of your program should be a division.
And you misspelled "onomatopoeia" in the comment on line 103.
I''m just guessing, of course, since you didn''t post anything that
would let us figure out what the actual problem might be.
There is no function in standard C called random(). The standard
defines a rand() function (whose implementation is typically not very
good, unfortunately), so using your system''s random() function might
give you better results. I presume you''ve read the documentation for
the random() function. It probably returns numbers in a wide range;
you''ll need to do something to scale them to the range you want.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
这篇关于随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!