rand()%n重新访问 [英] rand() % n Revisited

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

问题描述

快速rand()问题:


我知道你不应该使用" rand()%1024"例如,

因为它侧重于低位。然而,在我看来,由于这个论点不是2的幂(或者接近2美元的力量),所以这不是问题。高位将在结果中以较低的值同等地参与
。我错过了什么吗?


谢谢!


- Rich -

Quick rand() question:

I know you''re not supposed to use "rand() % 1024" for instance,
because it focuses on the lower bits. However, it seems to me that
given that the argument is not a power of two (or near a power of
two), that this is not an issue. The upper bits will participate
equally in the result with the lower. Am I missing something?

Thanks!

-- Rich --

推荐答案

10月22日下午6:04,Rich Fife< rf ... @ amug.orgwrote:
On Oct 22, 6:04 pm, Rich Fife <rf...@amug.orgwrote:

快速rand()问题:


我知道你不应该使用rand()%1024例如,

因为它侧重于低位。然而,在我看来,因为这个论点不是2的幂(或者接近两个b
的幂),所以这不是问题。高位将

平均参与较低的结果。我错过了什么?b $ b某事?
Quick rand() question:

I know you''re not supposed to use "rand() % 1024" for instance,
because it focuses on the lower bits. However, it seems to me
that given that the argument is not a power of two (or near a
power of two), that this is not an issue. The upper bits will
participate equally in the result with the lower. Am I missing
something?



是的,你缺少一些数学分析来支持你刚才所说的b $ b。如果您在Microsoft Visual C ++上使用(rand()%1023)或者/或
WATCOM C / C ++,那么32个可能的输出将有3%的额外偏差没有

问题你的随机数发生器有多好。我见过的没有C编译器'

rand()本身对随机

输出的影响比这更糟。


CLC关于这一点的常见问题解答给出了这个

点的极具误导性的建议,应该严重忽略它。如果你想认真地对待b
处理随机数字只需阅读我的网页:

http://www.pobox.com/~qed/random.html


我建立了a * REAL * ranged随机数生成器具有合理的

性能特征,最终在randrange()函数中使用
消除了远程随机数的所有主要问题。如果你需要纯粹的随机位质量的东西,你可以随时使用

Mersenne Twister或Fortuna作为我的发电机功能的基础。


-

Paul Hsieh
http://www.pobox.com/~qed/
http: //bstring.sf.net/

Yes, you are missing some mathematical analysis to back up what you
just said. If you do (rand() % 1023) on Microsoft Visual C++ or
WATCOM C/C++, 32 of the possible outputs will have an extra 3% bias no
matter how good your random number generator is. No C compiler''s
rand() that I have ever seen has, by itself, a worse effect on random
output than that.

The C.L.C. FAQ about this gives extremely misleading advice on this
point and it should seriously be ignored. If you want to seriously
deal with random numbers just read my page about it:

http://www.pobox.com/~qed/random.html

I build up a *REAL* ranged random number generator with reasonable
performance characteristics, culminating in the randrange() function
that removes all the primary problems with ranged random numbers. If
you want something with pure random bit quality you can always use the
Mersenne Twister or Fortuna as a base for my generator function.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/


Paul Hsieh< we ****** @ gmail.comwrote:
Paul Hsieh <we******@gmail.comwrote:

没有C编译器

rand(),我见过它本身对随机产生的影响更大

输出比起那个来说。
No C compiler''s
rand() that I have ever seen has, by itself, a worse effect on random
output than that.



那么你从来没有见过真正糟糕的BSD兰德(),这大大促成了兰德的偏执狂。有了它,rand()%1生成了序列0,

1,0,1,0,1,....

-

Larry Jones


这个游戏适合某些滥用行为。 - Calvin

Then you''ve never seen the truely bad BSD rand() that fostered most of
the paranoia about rand. With it, rand() % 1 generated the sequence 0,
1, 0, 1, 0, 1, ....
--
Larry Jones

This game lends itself to certain abuses. -- Calvin


On Thu,2008年10月23日10:46:39 -0400,lawrence.jones写道:
On Thu, 23 Oct 2008 10:46:39 -0400, lawrence.jones wrote:

Paul Hsieh< we ****** @ gmail.comwrote:
Paul Hsieh <we******@gmail.comwrote:

>没有C编译器的
rand()我所见过的,它本身对随机输出的影响比这更糟。
>No C compiler''s
rand() that I have ever seen has, by itself, a worse effect on random
output than that.



然后你从来没有见过真正糟糕的BSD兰德(),这大大促成了兰德的妄想症。有了它,rand()%1生成了序列0,

1,0,1,0,1,....


Then you''ve never seen the truely bad BSD rand() that fostered most of
the paranoia about rand. With it, rand() % 1 generated the sequence 0,
1, 0, 1, 0, 1, ....



这样会很糟糕,因为rand()%1应该只产生0.

That would be bad, as rand() % 1 should only ever produce 0.


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

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