rand()%range + 1的问题 [英] Problem with rand() % range+1

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

问题描述




我发现Google上的几个网站告诉我我不应该使用


rand() %范围+ 1


我应该使用类似的东西:


最低+ int(范围* rand()/(RAND_MAX) + 1.0))


他们未能明确原因。

低位或类似的东西似乎没那么随机。我不知道什么不那么随机性

意味着什么。是不是正常分发或类似的东西?

Hi,

I''ve found several sites on google telling me that I shouldn''t use

rand() % range+1

and I should, instead, use something like:

lowest+int(range*rand()/(RAND_MAX + 1.0))

They fail to make it clear why. There seems to be less randomness in the
lower bits or something like that. I don''t know what less randomness
would mean. Would it not be normally distributed or something like that?

推荐答案

8月25日,22:35,Rafael Cunha de Almeida< n .. 。@ email.provided>

写道:
On 25 Aug., 22:35, Rafael Cunha de Almeida <n...@email.provided>
wrote:




我已经在Google上发现了几个网站告诉我我不应该使用


* * * * rand()%range + 1


和相反,我应该使用类似的东西:


* * * *最低+ int(范围* rand()/(RAND_MAX + 1.0))


他们未能明确原因。

低位或类似的东西似乎没那么随机。我不知道什么不那么随机性

意味着什么。是不是正常分发或类似的东西?
Hi,

I''ve found several sites on google telling me that I shouldn''t use

* * * * rand() % range+1

and I should, instead, use something like:

* * * * lowest+int(range*rand()/(RAND_MAX + 1.0))

They fail to make it clear why. There seems to be less randomness in the
lower bits or something like that. I don''t know what less randomness
would mean. Would it not be normally distributed or something like that?



举一个随机生成器的例子,提供范围为

0..99的数字。如果你想要的数字在0..79范围内,那你的解决方案就是
给出的数字0..19的预期值是20..79范围的两倍。

所以如果你需要均匀分布你的方法是坏的(但是如果你只是想要一些随机分布,例如对于一个游戏,可能

)。

另一种方法更好,但并不完美。


/ Peter

Take an example of a random generator delivering numbers in the range
0..99. If you want numbers in the range 0..79, your solution would
give an expectancy of numbers 0..19 twice as high as the range 20..79.
So your method is bad if you require an even distribution (but might
be ok if you just want somewhat random distribution as eg for a game).
The other method is better, but not perfect.

/Peter


2008-08-25 16:35:21 -0400, Rafael Cunha de Almeida< no@email.providedsaid:
On 2008-08-25 16:35:21 -0400, Rafael Cunha de Almeida <no@email.providedsaid:

>

我在google上发现了几个网站告诉我我不应该使用


rand()%range + 1


我应该使用类似的东西:


最低+ int(范围* rand()/(RAND_MAX + 1.0))


他们未能明确原因。
>
I''ve found several sites on google telling me that I shouldn''t use

rand() % range+1

and I should, instead, use something like:

lowest+int(range*rand()/(RAND_MAX + 1.0))

They fail to make it clear why.



那是因为它是胡说八道。

That''s because it''s nonsense.


似乎随机性较低

较低位或类似的东西。我不知道什么不那么随机性

意味着什么。
There seems to be less randomness in the
lower bits or something like that. I don''t know what less randomness
would mean.



两者都有同样的问题。假装RAND_MAX为4

(也就是说,rand()返回0,1,2,3或4)并且范围也是4(

)是,你想要0,1,2或3的值。由于rand()生成五个不同的值,因此你不可能得到四个不同的值,并且具有相同的

似然。用铅笔和纸试试吧。两种方式。


您开始使用的值的数量必须是您希望得到的值的数量的完全倍数。这样做的方法是

扔掉rand()中的一些值,即那些超过

范围的值,该值小于RAND_MAX + 1。像这样:


int mult =(RAND_MAX + 1)/ range;

int max = range * mult;

int temp = rand();

while(max< temp)

temp = rand();

返回临时%范围;


现在,这并没有解决低

位中随机性较低的可能性,但这是一个单独的问题,取决于rand( )

实现。

Both have the same problem. Pretend for the moment that RAND_MAX is 4
(that is, rand() returns 0, 1, 2, 3, or 4) and range is also 4 (that
is, you want values of 0, 1, 2, or 3). Since rand() generates five
different values, you can''t get four distinct values with equal
likelihood. Try it with pencil and paper. Both ways.

The number of values that you start with has to be an exact multiple of
the number of values that you want to get. The way to do that is to
throw away some values from rand(), namely, those that exceed the
maximum multiple of range that''s less than RAND_MAX+1. Like this:

int mult = (RAND_MAX + 1) / range;
int max = range * mult;
int temp = rand();
while (max < temp)
temp = rand();
return temp % range;

Now, that doesn''t address the possibility of less randomness in the low
bits, but that''s a separate issue, and depends on how rand() is
implemented.


它是不是正常分发或类似的东西?
Would it not be normally distributed or something like that?



rand()产生均匀分布,而不是正态分布,并且

通常当有人在寻找限制范围时他们''还是

寻找统一分配。


-

Pete

Roundhouse Consulting,有限公司( www.versatilecoding.com )作者The

标准C ++库扩展:教程和参考

www。 petebecker.com/tr1book

rand() produces a uniform distribution, not a normal distribution, and
usually when someone is looking for a restricted range they''re still
looking for a uniform distribution.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


2008-08-25 17:00:48 -0400,peter koch< pe ** *************@gmail.com说:
On 2008-08-25 17:00:48 -0400, peter koch <pe***************@gmail.comsaid:

8月25日22:35,Rafael Cunha de Almeida< n ... @ email.provided>

写道:
On 25 Aug., 22:35, Rafael Cunha de Almeida <n...@email.provided>
wrote:

>

我有在谷歌发现了几个网站告诉我我不应该使用

?*?*?*?* rand()%range + 1

我应该使用类似的东西:

?*?*?*?*最低+ int(范围* rand()/(RAND_MAX + 1.0))

他们未能明确原因。在低位或类似的东西中似乎没有更少的随机性。我不知道
意味着什么不那么随意。是不是正常分发或类似的东西?
>Hi,

I''ve found several sites on google telling me that I shouldn''t use

?* ?* ?* ?* rand() % range+1

and I should, instead, use something like:

?* ?* ?* ?* lowest+int(range*rand()/(RAND_MAX + 1.0))

They fail to make it clear why. There seems to be less randomness in the
lower bits or something like that. I don''t know what less randomness
would mean. Would it not be normally distributed or something like that?



以一个随机发生器为例,提供范围为

0..99的数字。如果你想要的数字在0..79范围内,那你的解决方案就是
给出的数字0..19的预期值是20..79范围的两倍。

所以如果你需要均匀分布你的方法是坏的(但是如果你只是想要一些随机分布,例如对于一个游戏,可能

)。

另一种方法更好,但不完美。


Take an example of a random generator delivering numbers in the range
0..99. If you want numbers in the range 0..79, your solution would
give an expectancy of numbers 0..19 twice as high as the range 20..79.
So your method is bad if you require an even distribution (but might
be ok if you just want somewhat random distribution as eg for a game).
The other method is better, but not perfect.



嗯,另一种方法更好,因为它使非随机性不那么明显。 < gBut具有相同的样本范围,它仍然产生
二十个值是其余值的两倍;只是因为他们不再是二十个最低的那个了。


概率从根本上说只算数。如果你可以将

问题减少到足够小的值,那么你需要做的就是计算结果。
结果。对于rand()可以产生的每个可能的值,上面的每个公式产生的值是多少?b $ b值?对于rand()生成0..99范围内的数字来做

是有点乏味的,但对于0..9,有一个

目标范围0..7,它不会超过几分钟。不要相信你的直觉,直到你经历过几个例子,并且看到他们的确如何工作。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:一个教程和参考的作者
www.petebecker.com/tr1book

Well, the other method is better in that it makes the non-randomness
less obvious. <gBut with the same sample ranges it still produces
twenty values twice as often as the rest; it''s just that they''re not
the twenty lowest ones any more.

Probability is fundamentally just counting. If you can reduce the
problem to a small enough set of values, all you need to do is count
the results. For each possible value that rand() can produce, what
value does each of the formulae above produce? It''s a bit tedious to do
for rand() generating numbers in the range 0..99, but for 0..9, with a
target range of 0..7, it won''t take more than a few minutes. Don''t
trust your intuition until you''ve gone through several examples and
seen how they really work.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于rand()%range + 1的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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