how reliable is rand() ?
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: how reliable is rand() ?

1. Member
Join Date
Apr 2013
Posts
34

## 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?

2. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,735

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

3. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: how reliable is rand() ?

rand() is very reliable.

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).
Last edited by OReubens; September 2nd, 2013 at 06:51 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•