I'm interested for C++, though I suspect that simply imports the C standard definition. I believe the answer is no for what the standard says, but I'm most interested in the in-practice answer.
If RAND_MAX is always (2^n)-1, that simplifies dealing with an issue that turned up recently moving code from MinGW GCC to Linux GCC. RAND_MAX seems to be bigger (I didn't check, but possibly equal to INT_MAX or whatever the symbol is), so some old naively written RAND_MAX-isn't-big-enough-so-work-around-it code backfired. Now I need to decide just how general I need this library to be, considering the fiddliness of writing code that copes correctly with the possibility of overflow without making assumptions about e.g. the width of an int.
Anyway, are there any reasonably widely used C++ compilers that use something other than (2^n)-1 for RAND_MAX?
Also, am I correct that ((RAND_MAX | (RAND_MAX >> 1)) == RAND_MAX) is always and only true if RAND_MAX is equal to ((2^n)-1) for some unsigned integer n. I believe RAND_MAX is technically an int, but it makes no sense to have a negative or fractional value, so I think I can safely discount those. Bit-fiddling doesn't normally bother me, but I keep thinking the expression looks wrong, and I can't figure out why.
Finally, although I'm not going to be happy until I've got a working solution of my own, what should I be using for random numbers rather than write it myself? I need random numbers in the range 0 <= x < parameter, and I especially want as-equal-as-sanely-possible probabilities for all numbers. For example, taking (rand() % upperbound) gives a bias towards smaller values, especially when the upperbound is large - I want to avoid that.
Is there a Boost or C++0x thing for that?
EDIT
Following something in the "Related" bit on the side of the page shows there is indeed a way to get random numbers with given lower and upper bounds in boost.
In the standard GNU header file, stdlib.h, there is a definition of RAND_MAX which should be the sole definition in your system:
The value 2147483647 = 0x7fffffff is the largest unsigned 32-bit integer.
The rand functions, defined in stdlib.h, are reliable in standard GNU environments with one caveat: multi-threading (pthread) environments will need pthread code to synchronize in order to protect the rand(), srand(), and rand_r() calls since they are not reentrant. See the man page for srand for an explanation.
RAND_MAX should not be defined anywhere else in your system. If you are seeing a different value for RAND_MAX or seeing a definition of RAND_MAX somewhere besides stdlib.h, then this must be a non-standard, non-portable environment. Windows is notorious for making standardization and portability difficult (E.g. sockets implementation and APIs).
I don't know any implementation for which RAND_MAX is not one less than a power of two, but that isn't mandated by the standard;
((RAND_MAX | (RAND_MAX >> 1)) == RAND_MAX) is indeed a way to test if RAND_MAX is one less than a power of two.
I'm using
to make as evenly distributed as possible random numbers from rand().
According to both ISO C (C99 to C17 at least) and POSIX (Issue 7, 2016 Edition),
RAND_MAX
shall be at least 32767, i.e. 2^15−1, and there are no other restrictions.Under GNU/Linux,
RAND_MAX
is guaranteed to be exactly 2147483647 = 2^31−1 on all architectures for consistency with POSIX concerning therandom()
function: POSIX specifies that the return value is in the range from 0 to 2^31−1, and the Linux random(3) man page documents the range as being from 0 toRAND_MAX
.I don't know what the guarantees on
RAND_MAX
are, but you'd better avoid it if possible because of the number of broken implementations around and because it starts cycling quite quickly in today's applications. Getting a uniform distribution is described here.I recommend Boost.Random instead. The Mersenne twister generator represents a good tradeoff between speed, memory use and quality.
In Embacadero C++ Builder, there are two variables defined in stdlib.h:
In GCC (4.6) RAND_MAX = 0x7FFFFFFF (31bit) In MS Visual Studio (2012) RAND_MAX = 0x7FFF (15bit)