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

问题描述

我需要在C中生成0到1之间的两个均匀随机数?

怎么做?


我研究了rand函数你在哪里需要#define RAND_MAX为1

但这个rand函数会给我统一分布和唯一的

数字吗?

I need to generate two uniform random numbers between 0 and 1 in C ?
How to do it ?

I looked into rand function where you need to #define RAND_MAX as 1
but will this rand function give me uniformly distributed and unique
numbers ?

推荐答案



如果您有现代硬件和IEEE浮点数,我建议

dsfmt:


否则WELL512a.c和WELL512a.h来自:


是另一种可能性。

If you have modern hardware and IEEE floating point, I recommend
dsfmt:
http://www.math.sci.hiroshima-u.ac.j...FMT/index.html
otherwise WELL512a.c and WELL512a.h from here:
http://www.iro.umontreal.ca/~panneton/WELLRNG.html

Are another possibility.



这是一个只读值。这不是你设置的东西,

这是你要查询的内容。

That is a read-only value for you. It''s not something that you set,
it is something that you inquire upon.




常见问题解答中的有用信息从大约13.15开始




一组数字不应该是随机且唯一的。随机数生成器的一个重要的

属性是可以重复数字。要

提供我理解你想要的东西,请查看shuffle。如

13.19

There is useful information in the FAQ starting at about 13.15

http://www.c-faq.com/lib/index.html

A set of numbers shouldn''t be random AND unique. One of the important
properties of a random number generator is that numbers can be repeated. To
provide what I understand you to want, look into "shuffle" as described in
13.19




RAND_MAX告诉你最大随机数是多少。它没有b $ b b让你控制它。

RAND_MAX tells you what the maximum random number is. It doesn''t
let you control it.



rand()返回0到RAND_MAX之间的整数。打算统一分配

(虽然标准看起来并不像
这样说)所以你可以得到均匀分布的随机浮点

数字类似


((double)rand())/ RAND_MAX


如果你真的需要*独特*随机数你必须要b / b $ b做一些更复杂的事情。你确定你真的这么做吗?


- Richard

-

:wq

rand() returns an integer between 0 and RAND_MAX. It''s intended
to be uniformly distributed (though the standard doesn''t seem to
say so) so you can get uniformly distributed random floating-point
numbers by something like

((double)rand()) / RAND_MAX

If you really need *unique* random numbers you''re going to have to
do something more complicated. Are you sure you really do?

-- Richard
--
:wq


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

05-28 18:25
查看更多