[英]Is RAND_MAX guranteed to be <= UINT_MAX?
I'm trying to determine if RAND_MAX can fit inside an unsigned int variable.我正在尝试确定 RAND_MAX 是否可以放入 unsigned int 变量中。 After looking through the C99 standard, I have only found that RAND_MAX is guaranteed to have a value of at least 32767. While I know that RAND_MAX expands to int value, I'm not sure if it's a long int.翻阅C99标准后,我只发现RAND_MAX保证值至少为32767。虽然我知道RAND_MAX扩展为int值,但我不确定它是否是long int。 Right now I'm working with unsigned long int variables, but I would like to simplify my source code to use unsigned int variables.现在我正在使用 unsigned long int 变量,但我想简化我的源代码以使用 unsigned int 变量。
To summarize, is this assumption statement true for the C99 standard:总而言之,这个假设陈述是否适用于 C99 标准:
RAND_MAX <= UINT_MAX <= ULONG_MAX RAND_MAX <= UINT_MAX <= ULONG_MAX
Also, is RAND_MAX is explicitly less than (ie not equal to) UINT_MAX or ULONG_MAX.此外,RAND_MAX 是否明确小于(即不等于)UINT_MAX 或 ULONG_MAX。 I realize that the value of RAND_MAX is often a Mersenne prime, but I'm not sure if this is a standard.我意识到 RAND_MAX 的值通常是梅森素数,但我不确定这是否是标准。
RAND_MAX
is the maximum value that can be returned by rand()
. RAND_MAX
是rand()
可以返回的最大值。 Since rand()
is defined as returning int
, RAND_MAX
will be no more than INT_MAX
, and therefore also no more than UINT_MAX
.由于rand()
被定义为返回int
, RAND_MAX
将不超过INT_MAX
,因此也不超过UINT_MAX
。
rand
has signature int rand(void);
rand
有签名int rand(void);
, so any non-negative value it returns must fit inside an unsigned int
, since the non-negative range of int
is contained in the range of unsigned int
.的,因此任何非负值它返回必须适合的内部unsigned int
,由于非负范围int
被包含在范围内unsigned int
。
This doesn't explicitly state that RAND_MAX
is <= INT_MAX
, but the requirement that rand
return values in the range 0
to RAND_MAX
along with the lower limit of 32767 for RAND_MAX
indicates that rand
is expected to cover the range 0
to RAND_MAX
, which would not be the case if it were greater than INT_MAX
.这不明确状态RAND_MAX
是<= INT_MAX
,但要求该rand
范围内的返回值0
到RAND_MAX
与32767下限沿着RAND_MAX
表示rand
预期覆盖范围0
至RAND_MAX
,这将如果它大于INT_MAX
则情况并非INT_MAX
。
I agree with the answerer to this question that the Mersenne Twister will give you all the control and speed you need.我同意这个问题的回答者的观点,即 Mersenne Twister 将为您提供所需的所有控制和速度。 It's also a very easy addition to any C++ program.它也是任何 C++ 程序的一个非常简单的补充。
But like many others have said, rand()'s return type of int virtually guarantees RAND_MAX to be less than UINT_MAX.但正如许多其他人所说,rand() 的 int 返回类型实际上保证了 RAND_MAX 小于 UINT_MAX。
Is
RAND_MAX
guaranteed to be <=UINT_MAX
?RAND_MAX
是否保证 <=UINT_MAX
?
Yes.是的。
32767 <= RAND_MAX <= INT_MAX <= UINT_MAX
65535 <= UINT_MAX
... is
RAND_MAX
is explicitly less than (ie not equal to)UINT_MAX
orULONG_MAX
(?) ...RAND_MAX
是否明确小于(即不等于)UINT_MAX
或ULONG_MAX
(?)
No. RAND_MAX
may equal UINT_MAX
and ULONG_MAX
in uncommon implementations.不。在不常见的实现中, RAND_MAX
可能等于UINT_MAX
和ULONG_MAX
。
Commonly RAND_MAX <= UINT_MAX/2
, yet that is not specified by the C Library spec.通常RAND_MAX <= UINT_MAX/2
,但 C 库规范并未指定。
To assert this common case:要断言这种常见情况:
#include <limits.h>
#include <stdlib.h>
_Static_assert(RAND_MAX <= UINT_MAX/2, "Large RAND_MAX");
I realize that the value of
RAND_MAX
is often a Mersenne prime, but I'm not sure if this is a standard.我意识到RAND_MAX
的值通常是梅森素数,但我不确定这是否是标准。
Not quite.不完全的。 RAND_MAX
is commonly a Mersenne number - a power of 2 minus 1. It is not part of the C library standard, yet commonly a very desirable property. RAND_MAX
通常是一个梅森数- 2 减 1 的幂。它不是 C 库标准的一部分,但通常是一个非常理想的属性。
To assert this desirable property:要断言此理想属性:
#include <stdlib.h>
_Static_assert((RAND_MAX & 1) && ((RAND_MAX/2 + 1) & (RAND_MAX/2)) == 0,
"RAND_MAX is not a Mersenne number");
Sometimes RAND_MAX
is a Mersenne prime .有时RAND_MAX
是梅森素数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.