开发者

why is RAND_MAX a macro in C++?

开发者 https://www.devze.com 2023-01-18 01:06 出处:网络
Why is it not a const? I think this is no开发者_运维问答t a clear C++ way. Perhaps there is a more C++ way to generate random numbers, is there?RAND_MAX comes from the C standard library, where it is

Why is it not a const? I think this is no开发者_运维问答t a clear C++ way. Perhaps there is a more C++ way to generate random numbers, is there?


RAND_MAX comes from the C standard library, where it is defined as a macro.

In C, macros are the way manifest constants are defined. A const object isn't actually a constant in C (this means a const object cannot be used in constant expressions).


If you're looking for a "more C++-way", you could use 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.


It's a macro because it comes from C where it's been a macro for a long time.

  1. Boost random would be one alternative.
  2. TR1's random number generation classes would be another.
  3. Unlike most of TR1, the PRNG classes are being completely revised for C++0x.
0

精彩评论

暂无评论...
验证码 换一张
取 消