开发者

What is a suitable replacement for rand()?

As far as I know rand() does not generate a uniform random distribution. What function/algorithm will allo开发者_高级运维w me to do so? I have no need for cryptographic randomness, only a uniform random distribution. Lastly, what libraries provide these functions? Thanks!


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜