What is a suitable replacement for rand ()?

As far as I know, rand () does not generate a uniform random distribution. What function / algorithm will allow me to do this? I do not need cryptographic randomness, only a uniform random distribution. And finally, which libraries provide these features? Thanks!

+1
c random
source share
4 answers

rand() creates a uniform (pseudo) random distribution.

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

The rand function computes a sequence of pseudo-random integers ranging from 0 to RAND_MAX .

where RAND_MAX is at least 32767. This is admittedly vague, but the goal is to give you an even distribution - and in practice, what it actually implements.

The standard provides an example implementation, but the C implementation is not required to use it.

In practice, there are, of course, the best random number generators. And one specific requirement for rand() is that it must produce exactly the same sequence of numbers for a given seed ( srand() argument). Your description does not mean that it will be a problem for you.

One of the problems is that rand() gives evenly distributed numbers in a fixed range. If you need numbers in a different range, you need to do extra work. For example, if RAND_MAX is 32767, then rand() can display 32768 different values; you cannot get random numbers in the range 0..9 without discarding some values, since there is no way to evenly distribute these 32768 different values ​​into 10 codes of equal size.

Other PRNGs are more likely to give you better results than rand() , but they will still be subject to the same problems.

As usual, the comp.lang.c FAQ answers this better than I do; see questions from 13.15 to 13.21.

+2
source share

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

Whenever this question arises, someone answers that you should not use your own random number generator, but should leave it to the experts. I answer that you should not come up with your own algorithm. Leave it to specialists, because it is really very subtle. But this is normal and even useful for your own implementation. This way you know what is being done, and you can use the same method in different languages ​​or platforms.

The algorithm in this article is George Marsaglia, chief expert on random number generation. Although the code is tiny, the method works well for standard tests.

+2
source share

The BSD random() function (included in the XSI option for POSIX / SUS) is almost universal and much better than rand for most systems (except for some where rand actually uses random and thus both are very good).

If you prefer to go beyond system libraries, here are some good details about your options:

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

(From Michael Niedermeier of FFmpeg Glory.)

+2
source share

Well, the question of whether or not the actual pseudo-random generator exists is still open . However, a quick search reveals that there may be slightly easier alternatives .

0
source share

All Articles