生成随机数 [英] Generating random numbers

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

问题描述

问候,


这是我到目前为止的第二篇文章。感谢第一篇文章给我的所有建议。


现在我遇到生成随机数的问题。我知道如果我们想生成一个随机数,我们可以使用:


int tempNUM = rand();


但是,一旦我执行程序,生成的数字总是相似的,我从互联网上找到了这个。它说需要添加这一行来使数字生成不同:


srand((无符号)时间(0));


当我添加该行时,我的结果就像地狱一样..有人可以请赐教。谢谢你

Greetings,

This is my second post till now. Thanks for all the advice given to me for the first post.

Now I''m having problem with generating random numbers. I know if we want to generate a random number, we can use:

int tempNUM = rand();

But, once I execute the program, the generated number is always similar, and I found this from the internet. It says need to add this line to make the numbers generated differently:

srand((unsigned)time(0));

When I add that line, my result is like hell.. Could someone please kindly enlighten me. Thank you

推荐答案

你得到的结果到底是什么?
What kind of hell is the result you get?


如果你想限制随机的结果您可以使用模块运算符编号。


Number = rand()%200 //从0-199生成一个数字
If you want to limit the results from the random number you can use the modules operator.

Number = rand() % 200// generates a number from 0-199


是的,你实际上并没有生成一个 ;随机数本身,但更多的是伪随机数。有一些算法可以得到一个非常分散的范围,一些是积极的,一些是负面的......等等。一个很酷的事情是,如果你设置相同数量的种子,你可以拿出相同的随机数是时候复制实验了。


你可能得到一个'随机数'。'你需要它之​​间的特定范围吗? (啊,请看上面的Studlyami的帖子,了解如何让它在一个范围内)
Yeah, you''re not actually generating a "random number" per se, but more a pseudo-random number. There are algorithms for getting a very dispersed range, some positive, some negative, etc.... One cool thing is that if you set the seed with the same number, you can come up with the same ''random numbers'' each time to duplicate experiments.

You probably are getting a ''random number.'' Is there a specific range that you need it be between? (Ah, see Studlyami''s post above for how to get it to be in a range)


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

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