c++ - Is the value of RAND_MAX always (2^n)-1? -
i'm interested c++, though suspect imports c standard definition. believe answer no standard says, i'm interested in in-practice answer.
if rand_max (2^n)-1, simplifies dealing issue turned moving code mingw gcc linux gcc. rand_max seems bigger (i didn't check, possibly equal int_max or whatever symbol is), old naively written rand_max-isn't-big-enough-so-work-around-it code backfired. need decide how general need library be, considering fiddliness of writing code copes correctly possibility of overflow without making assumptions e.g. width of int.
anyway, there reasonably used c++ compilers use other (2^n)-1 rand_max?
also, correct ((rand_max | (rand_max >> 1)) == rand_max) , true if rand_max equal ((2^n)-1) unsigned integer n. believe rand_max technically int, makes no sense have negative or fractional value, think can safely discount those. bit-fiddling doesn't bother me, keep thinking expression looks wrong, , can't figure out why.
finally, although i'm not going happy until i've got working solution of own, should using random numbers rather write myself? need random numbers in range 0 <= x < parameter, , want as-equal-as-sanely-possible probabilities numbers. example, taking (rand() % upperbound) gives bias towards smaller values, when upperbound large - want avoid that.
is there boost or c++0x thing that?
edit
following in "related" bit on side of page shows there indeed way random numbers given lower , upper bounds in boost.
i don't know guarantees on rand_max
are, you'd better avoid if possible because of number of broken implementations around , because starts cycling quite in today's applications. getting uniform distribution described here.
i recommend boost.random instead. mersenne twister generator represents tradeoff between speed, memory use , quality.
Comments
Post a Comment