Is the value of RAND_MAX always (2^n)-1?

2019-01-26 08:52发布

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.

标签: c++ random
7条回答
再贱就再见
2楼-- · 2019-01-26 09:14

In the standard GNU header file, stdlib.h, there is a definition of RAND_MAX which should be the sole definition in your system:

#define RAND_MAX    2147483647

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).

查看更多
该账号已被封号
3楼-- · 2019-01-26 09:17
  • 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

    int alea(int n){ 
      assert (0 < n && n <= RAND_MAX); 
      int partSize = 
        n == RAND_MAX ? 1 : 1 + (RAND_MAX-n)/(n+1); 
      int maxUsefull = partSize * n + (partSize-1); 
      int draw; 
      do { 
        draw = rand(); 
      } while (draw > maxUsefull); 
      return draw/partSize; 
    }
    

to make as evenly distributed as possible random numbers from rand().

查看更多
迷人小祖宗
4楼-- · 2019-01-26 09:22

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 the random() 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 to RAND_MAX.

查看更多
冷血范
5楼-- · 2019-01-26 09:27

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.

查看更多
劫难
6楼-- · 2019-01-26 09:32

In Embacadero C++ Builder, there are two variables defined in stdlib.h:

/* Maximum value returned by "rand" function*/
#define RAND_MAX  0x7FFFU

/* Maximum value returned by "_lrand" function (also used by random() macro)*/
#define LRAND_MAX 0x7FFFFFFFU
查看更多
不美不萌又怎样
7楼-- · 2019-01-26 09:38

In GCC (4.6) RAND_MAX = 0x7FFFFFFF (31bit) In MS Visual Studio (2012) RAND_MAX = 0x7FFF (15bit)

查看更多
登录 后发表回答