C++ - C - RAND_MAX

C++ - C - 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值