随机数 [英] random number

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

问题描述

我想获得一个0到1之间的随机数。

以下代码可以工作,但在我看来是一个小小的

尴尬。有没有更好的解决方案。


double rnd;

int integerRnd;


srand(static_cast< unsigned>(time(NULL) )));

for(int i = 0; i< 9; ++ i){

// rand()返回0到32767之间的值

rnd = 10000./ rand();

//商的整数部分

integerRnd = rnd;

//每个随机数,形式为0.xxx

rnd - = integerRnd;

cout<< rnd<< endl;

}

解决方案

Roman T?ngi写道:

我想得到一个介于0和1之间的随机数。
以下代码有效,但在我看来似乎很尴尬。有没有更好的解决方案。




将[0,n]范围内的值减少到范围[0,1}

的常用方法是除以n + 1。如果你希望1在最终范围内,则除以n




-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Roman T?ngi写道:

我想得到0到1之间的随机数。
以下代码有效,但在我看来似乎很尴尬。有没有更好的解。


有很多解决方案。你试过网络了吗?

你不知道你需要什么发行版。对于统一的一个

,您只需缩放从''rand''函数获得的数字

(参见comp.lang.c常见问题解答)。对于其他

分布,还有其他方法。同样,在这种情况下,网络是你朋友的


[..]



Pete Becker写道:

Roman T?ngi写道:

我想获得0到1之间的随机数。
以下代码有效但在我看来似乎很尴尬。有没有更好的解决方案。



将[0,n]范围内的值减少到范围[0,1}
的常用方法是除以n + 1。如果你希望1在最终范围内,则除以n




忘了提及,以下评论:


// rand()返回0到32767之间的值


不正确。兰德的一些实现做到了,其他人不做。看看

了。


-


Pete Becker

Dinkumware,有限公司( http://www.dinkumware.com


I want to get a random number between 0 and 1.
The following code works but it seems to me a litte
awkward. Is there a "better" solution.

double rnd;
int integerRnd;

srand(static_cast<unsigned>(time(NULL)));
for (int i = 0; i < 9; ++i) {
// rand() returns a value from 0 to 32767
rnd = 10000. / rand();
// integer part of quotient
integerRnd = rnd;
// every random number in the form of 0.xxx
rnd -= integerRnd;
cout << rnd << endl;
}

解决方案

Roman T?ngi wrote:

I want to get a random number between 0 and 1.
The following code works but it seems to me a litte
awkward. Is there a "better" solution.



The usual way to reduce values in the range [0, n) to the range [0, 1)
is to divide by n+1. If you want 1 to be in the final range, divide by n
instead.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


Roman T?ngi wrote:

I want to get a random number between 0 and 1.
The following code works but it seems to me a litte
awkward. Is there a "better" solution.
There are many solutions out there. Have you tried the web?
You don''t say what distribution you need. For uniform one
you can simply scale the numbers you get from ''rand'' function
(see comp.lang.c FAQ for the recommendations). For other
distributions there are other approaches. Again, the web is
your friend in that case.
[..]



Pete Becker wrote:

Roman T?ngi wrote:

I want to get a random number between 0 and 1.
The following code works but it seems to me a litte
awkward. Is there a "better" solution.



The usual way to reduce values in the range [0, n) to the range [0, 1)
is to divide by n+1. If you want 1 to be in the final range, divide by n
instead.



Forgot to mention, the following comment:

// rand() returns a value from 0 to 32767

is incorrect. Some implementations of rand do that, others don''t. Look
it up.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


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

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