Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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 (where n is some natural number - in effect the number of random binary digits in a random number), 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • 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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...