随机数? [英] Random Numbers?

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

问题描述

您好,

我正在尝试使用C获取一个随机数。我尝试过使用随机数(),

但我的程序一直在生成数字我输入的数字超出了范围。


有人有任何建议吗?


Kyle

-

comp.lang.c.moderated - 审核地址: cl ** @ plethora.net

解决方案

Kyle写道:

你好,我正在尝试使用C得到一个随机数。我已经尝试过使用random(),
但是我的程序不断产生超出我输入数字范围的数字。




见,例如:

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


(以及常见问题解答中的其他附近条目)。


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀


2004年1月13日格林尼治标准时间02:40:40, sh******@msn.com ( Kyle)写道:

你好,
我正在尝试使用C获取一个随机数。我尝试过使用随机数(),
但是我的程序产生的数字超出了我输入的数字范围。

任何人都有任何建议吗?

Kyle




使用模数运算符(%)来表示数字的范围。


苏珊

-

comp.lang.c.moderated - 审核地址: cl**@plethora.net


sh******@msn.com (Kyle)写道:

我正在尝试使用C得到一个随机数。我尝试过使用随机数(),
但是我的程序一直在生成数字我输入的数字远远超出了范围。

任何人都有任何建议吗?




在线乘法你的程序中有76个应该是一个部门。

你拼写错误的onomatopoeia在第103行的评论中。


我当然只是在猜测,因为你没有发布任何

会让我们想到的东西实际问题可能是什么。


标准C中没有函数叫做random()。标准

定义了一个rand()函数(不幸的是,它的实现通常不是很好),所以使用你的系统的random()函数可能

给你更好的结果。我猜你已经阅读了

random()函数的文档。它可能返回范围很广的数字;

你需要做一些事情来将它们扩展到你想要的范围。


-

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

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

Schroedinger做莎士比亚:要*和*不要

-

comp.lang.c.moderated - 审核地址: cl**@plethora.net


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

解决方案

Kyle wrote:

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.



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


On 13 Jan 2004 02:40:40 GMT, sh******@msn.com (Kyle) wrote:

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



Use the modulus operator (%) to bring the number in range.

Susan
--
comp.lang.c.moderated - moderation address: cl**@plethora.net


sh******@msn.com (Kyle) writes:

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?



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


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

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