如何在C中生成随机数 [英] How to generate random numbers in C

查看:107
本文介绍了如何在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 ?

推荐答案

4月10日,9:54 * pm,pereges< Brol ... @ gmail.comwrote:
On Apr 10, 9:54*pm, pereges <Brol...@gmail.comwrote:

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

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



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

dsfmt:
http://www.math.sci.hiroshima -u.ac.j ... FMT / index.html

否则WELL512a.c和WELL512a.h来自: http://www.iro.umontreal.ca/~panneton/WELLRNG.html


是另一种可能性。

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.


我查看了rand函数,你需要#define RAND_MAX为1

但是这个兰特功能会给我*均匀分布和独特的

数字吗?
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 ?



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

这是你要查询的内容。

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


" pereges"写道:
"pereges" writes:

>我需要在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 ?



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

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


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

属性是可以重复数字。要

提供我理解你想要的东西,请查看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


文章< 9c ****************** **************** @ s33g2000pri。 googlegroups.com>,

pereges< Br ***** @ gmail.comwrote:
In article <9c**********************************@s33g2000pri. googlegroups.com>,
pereges <Br*****@gmail.comwrote:

>我查看了rand函数你需要在哪里#define RAND_MAX为1
>I looked into rand function where you need to #define RAND_MAX as 1



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

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


>但这个rand函数会给我统一分布和唯一的
数字?
>but will this rand function give me uniformly distributed and unique
numbers ?


>我需要在C中生成0到1之间的两个均匀随机数?
>I need to generate two uniform random numbers between 0 and 1 in C ?



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中生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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