If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

how reliable is rand() ?

how reliable is rand() ?

I want to make a statistic info gathering program, I want it to run millions (perhaps billions) of random scenario's
and record the percentages/averages, yes I know I could do it mathematically quite easily in most cases, but in other cases it's easier just to run a simulation (especially if you need a fair amount of data)

would rand() be good for this? or does rand() loop after a while? and if so, after how many digits?

Re: how reliable is rand() ?

The exact algorithm used for rand depends on the standard library implementation. In your case, you might want to use an algorithm with known characteristics, e.g., some version of Mersenne Twister, which is available in the C++11 standard library via the <random> header.

Re: how reliable is rand() ?

it is however unsuitable for just about anything. It's period is too short (typically 32k although some exceptions exist), and the number of bits it has is too few (again, typically 15 bits).

For "real" work you need a PRNG that generates over a longer period and has more bits. Note that all PRNG's end up generating "predictable" patterns that may show up as artefacts depending on how they're being used.

Some applications need "more randomness", while others need "higher period" and still others need "crytographically secure", and some prefer (or prefer against) uniformity (yes, as odd as that may seem).

if you're "unsure" and just want "as random as possible", a 32bit mersenne twister will probably be a good place to start. 32bit MT will probably be sufficient, but I've worked on a project that needed a 128bit MT so unless you can provide more info on your needs, it's impossible to give solid advice.

You' question is formulated strange in that sense. do you need millions of generated random numbers. or do you need millions (bilions) of different random number sequences.

the one is a matter of periodicity, the 2Nd is a matter of seeding (and choice of algorithm).