C / C++ -
RAND_MAX
RAND_MAX
1. C - RAND_MAX
Defined in header <stdlib.h>
- 定义于头文件 <stdlib.h>
#define RAND_MAX /*implementation defined*/
Expands to an integer constant expression equal to the maximum value returned by the function rand()
. This value is implementation dependent. It’s guaranteed that this value is at least 32767
.
展开成等于函数 rand()
最大返回值的整数常量表达式。此值为实现定义。标准保证此值至少为 32767
。
2. C++ - RAND_MAX
Defined in header <cstdlib>
- 定义于头文件 <cstdlib>
Maximum value returned by rand
- rand
返回的最大值
This macro expands to an integral constant expression whose value is the maximum value returned by the rand
function.
这个宏扩展为整数常量表达式,其值是 rand
函数返回的最大值。
This value is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation.
该值取决于库,但在任何标准库实现中,保证至少为 32767。
Expands to an integer constant expression equal to the maximum value returned by the function std::rand
. This value is implementation dependent. It’s guaranteed that this value is at least 32767
.
展开成等于函数 std::rand
返回的最大值的整数常量表达式。此值依赖实现。保证此值至少为 32767
。
3. Example
//============================================================================
// Name : srand
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int random_variable = 0;
srand(time(0)); // use current time as seed for random generator
random_variable = rand();
printf("Random value on [0,%d]: %d\n", RAND_MAX, random_variable);
return 0;
}
Random value on [0,2147483647]: 1345030193
Reference
http://www.cplusplus.com/reference/cstdlib/RAND_MAX/
https://en.cppreference.com/w/c/numeric/random/RAND_MAX
https://en.cppreference.com/w/cpp/numeric/random/RAND_MAX