- 主题:问smthbbs源代码里面的rand函数是什么?
是bbs自己定义的,还是标准c函数?
rand()%10什么意思?
--
FROM 162.105.27.*
居然发现了
rand() % 100000000
看来我可以放心用了。
【 在 yanzg (油炸鬼~新家~项王) 的大作中提到: 】
: 是bbs自己定义的,还是标准c函数?
: rand()%10什么意思?
--
FROM 162.105.27.*
这个你在哪里看到的?rand()在很多系统上最大只返回三万多,%100000000有什么意义?
而且rand()函数在很多系统上实现都有低字节随机度不够的问题。man rand明确说了:
In Numerical Recipes in C: The Art of Scientific Computing (William H.
Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling;
New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the
following comments are made:
"If you want to generate a random integer between 1 and 10, you
should always do it by using high-order bits, as in
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
【 在 yanzg (油炸鬼~新家~项王) 的大作中提到: 】
: 居然发现了
: rand() % 100000000
: 看来我可以放心用了。
: ...................
--
FROM 128.12.181.*
水木的源代码
src下面
【 在 atppp (Big Mouse) 的大作中提到: 】
: 这个你在哪里看到的?rand()在很多系统上最大只返回三万多,%100000000有什么意义?
: 而且rand()函数在很多系统上实现都有低字节随机度不够的问题。man rand明确说了:
: In Numerical Recipes in C: The Art of Scientific Computing (William H.
: ...................
--
修改:yanzg FROM 162.105.27.*
FROM 162.105.27.*
哇!
【 在 atppp (Big Mouse) 的大作中提到: 】
: 这个你在哪里看到的?rand()在很多系统上最大只返回三万多,%100000000有什么意义?
: 而且rand()函数在很多系统上实现都有低字节随机度不够的问题。man rand明确说了:
: In Numerical Recipes in C: The Art of Scientific Computing (William H.
: ...................
--
FROM 59.66.199.*
Linux 的 rand() 返回的是 int,所以是 32-bit 的随机数,Windows 的 rand()
返回的是 short(),是 16-bit 的随机数。
不过直接用 rand() % 10000000 确实不好,估计有人没看过这段 man page 说明。
我以前写分散目录存储文章的时候,看过这段,所以没有直接 rand() % 52。
【 在 atppp (Big Mouse) 的大作中提到: 】
: 这个你在哪里看到的?rand()在很多系统上最大只返回三万多,%100000000有什么意义?
: 而且rand()函数在很多系统上实现都有低字节随机度不够的问题。man rand明确说了:
: In Numerical Recipes in C: The Art of Scientific Computing (William H.
: ...................
--
FROM 162.105.242.*
恩,我前面糊涂了。不够随机的问题,其实数目多的情况下没这么严重,但是%3 %4 这种小随机,用低位来做会产生明显的不均匀现象...前面提到那个是utmp key,对100000000取余,差不多是26位的强度一般来说足够强了...
【 在 flyriver (江~~忙碌生活) 的大作中提到: 】
: Linux 的 rand() 返回的是 int,所以是 32-bit 的随机数,Windows 的 rand()
: 返回的是 short(),是 16-bit 的随机数。
: 不过直接用 rand() % 10000000 确实不好,估计有人没看过这段 man page 说明。
: ...................
--
修改:atppp FROM 128.12.181.*
FROM 128.12.181.*