简体   繁体   English

为什么RAND_MAX是C ++中的宏?

[英]why is RAND_MAX a macro in C++?

Why is it not a const ? 为什么不是const I think this is not a clear C++ way. 我认为这不是一个清晰的C ++方法。 Perhaps there is a more C++ way to generate random numbers, is there? 也许还有一种更多的C ++方式来生成随机数,是吗?

RAND_MAX comes from the C standard library, where it is defined as a macro. RAND_MAX来自C标准库,在这里它被定义为宏。

In C, macros are the way manifest constants are defined. 在C语言中,宏是定义清单常量的方式。 A const object isn't actually a constant in C (this means a const object cannot be used in constant expressions). const对象实际上不是C中的常量(这意味着const对象不能在常量表达式中使用)。

If you're looking for a "more C++-way", you could use boost::random . 如果您正在寻找“更多的C ++方式”,则可以使用boost::random

Anyway, RAND_MAX is a macro, because it comes from "legacy C" rand() function, where using preprocessor symbols for declaring constants was a de-facto standard. 无论如何, RAND_MAX是一个宏,因为它来自“传统C” rand()函数,在该函数中,使用预处理器符号声明常量是事实上的标准。

It's a macro because it comes from C where it's been a macro for a long time. 这是一个宏,因为它来自C,在很长一段时间以来一直是宏。

  1. Boost random would be one alternative. 提高随机性将是一种选择。
  2. TR1's random number generation classes would be another. TR1的随机数生成类将是另一个。
  3. Unlike most of TR1, the PRNG classes are being completely revised for C++0x. 与大多数TR1不同,PRNG类已针对C ++ 0x进行了完全修订。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM