1

As far as I know rand() does not generate a uniform random distribution. What function/algorithm will allow me to do so? I have no need for cryptographic randomness, only a uniform random distribution. Lastly, what libraries provide these functions? Thanks!

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
chacham15
  • 13,719
  • 26
  • 104
  • 207
  • You need something like `unidrnd`, see http://octave.sourceforge.net/octave/function/unidrnd.html .. There is a C++ API for it as well I suppose, try digging out the code :) –  Sep 14 '11 at 18:54

4 Answers4

2

Here's an article and a stand-alone random number generator written in C#. The code is very small and easily portable to C++ etc.

Whenever this subject comes up, someone responds that you should not use your own random number generator but should leave that up to specialists. I respond that you should not come up with your own algorithm. Leave that up to specialists because it is indeed very subtle. But it's OK and even beneficial to have your own implementation. That way you know what's being done, and you could use the same method across languages or platforms.

The algorithm in that article is by George Marsaglia, a top expert in random number generation. Even though the code is tiny, the method holds up well to standard tests.

John D. Cook
  • 29,517
  • 10
  • 67
  • 94
2

The BSD random() function (included in the XSI option of POSIX/SUS) is almost universally available and much better than rand on most systems (except some where rand actually uses random and thus they're both pretty good).

If you'd rather go outside the system libraries, here's some good information on your choices:

http://guru.multimedia.cx/category/pseudo-random-number-generators/

(From Michael Niedermayer of FFmpeg fame.)

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
2

rand() does generate a uniform (pseudo-)random distribution.

The actual requirement, from the C standard (3.7 MB PDF), section 7.20.2.1, is:

The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.

where RAND_MAX is at least 32767. That's admittedly vague, but the intent is that it gives you a uniform distribution -- and in practice, that's what implementations actually do.

The standard provides a sample implementation, but C implementations aren't required to use it.

In practice, there are certainly better random number generators out there. And one specific requirement for rand() is that it must produce exactly the same sequence of numbers for a given seed (argument to srand()). Your description doesn't indicate that that would be a problem for you.

One problem is that rand() gives you uniformly distributed numbers in a fixed range. If you want numbers in a different range, you have to do some extra work. For example, if RAND_MAX is 32767, then rand() can produce 32768 distinct values; you can't get random numbers in the range 0..9 without discarding some values, since there's no way to evenly distribute those 32768 distinct values into 10 equal sized buckets.

Other PRNGs are likely to give you better results than rand(), but they're still probably going to be subject to the same issues.

As usual, the comp.lang.c FAQ answers this better than I did; see questions 13.15 through 13.21.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

Well, the question of whether or not an actual pseudorandom generator exists is still open. That being said, a quick search reveals that there may be some slightly better alternatives.

Community
  • 1
  • 1
  • Generally we settle for "no *known* efficient computation", with a side order of "and no research suggesting that one will be found any time soon". – Steve Jessop Sep 14 '11 at 22:10