2

I'm making a monte carlo simulation in C++ and I was using Boost for random numbers. I used GSL a bit too. But it turns out random number generation is one of my biggest runtime inefficiencies, so I just started using good old rand() from cstdlib.

How badly am I risking to have poor random number properties on my simulation? I use around 10^6 or 10^7 random number samples.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
  • Did you read the answers in [*about-rand-*](http://stackoverflow.com/questions/328950/c-the-definitive-truth-about-rand-random-and-arc4random) before posting :?) Especially Martin v. Löwis answer yields some details. A simple google query with `period of rand.c` showed it on the first page. Poor implementations and picking 10^6 numbers out of a range guaranteed to be only at least 32767 would not really be my first choice. What happened to these Mersenne-Twister implementations? Wasn't it a good compromise between speed and a long period? – Dilettant Jul 28 '11 at 17:32

1 Answers1

0

There are two issues: (1) because RAND_MAX is only guaranteed to be at least 32767, there might not be many possible values (not necessarily bad for some applications), and (2) poor implementations.

If you need what is known as a secure random number generator you will need to look somewhere else. But for many apps, rand() is sufficient.

A blog post that addresses your concerns is http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232