What difference between rand() and random() functions?
randomize()
and random()
are not part of the standard library. Perhaps your teacher wrote functions with these names for use in your class, or maybe you really mean random()
and srandom()
which are part of POSIX and not available on Windows. rand()
and srand()
are part of the standard library and will be provided by any standard conforming implementation of C++.
You should avoid rand()
and srand()
and use the new C++11 <random>
library. <random>
was added as part of the C++11 standard (and VS2012 does provide it).
Video explaining why: rand()
Considered Harmful
rand()
is typically a low quality pRNG and not suitable for applications that need a reasonable level of unpredictability.<random>
provides a variety of engines with different characteristics suitable for many different use cases.Converting the results of
rand()
into a number you can use directly usually relies on code that is difficult to read and easy to get wrong, whereas using<random>
distributions is easy and produces readable code.The common methods of generating values in a given distribution using
rand()
further decrease the quality of the generated data.%
generally biases the data and floating point division still produces non-uniform distributions.<random>
distributions are higher quality as well as more readable.rand()
relies on a hidden global resource. Among other issues this causesrand()
to not be thread safe. Some implementations make thread safety guarantees, but this is not required standard. Engines provided by<random>
encapsulate pRNG state as objects with value semantics, allowing flexible control over the state.srand()
only permits a limited range of seeds. Engines in<random>
can be initialized using seed sequences which permit the maximum possible seed data.seed_seq
also implements a common pRNG warm-up.
example of using <random>
:
#include <iostream>
#include <random>
int main() {
// create source of randomness, and initialize it with non-deterministic seed
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng{seed};
// a distribution that takes randomness and produces values in specified range
std::uniform_int_distribution<> dist(1,6);
for (int i=0; i<100; ++i) {
std::cout << dist(eng) << '\n';
}
}
Although there are (obviously, above) people who will assert with religious fervor that rand() is bad and random() is not, it turns out that your mileage may vary. Here's the gcc answer to the question of "What's the difference...", as supplied by the gcc version of stdlib.h (emphasis added):
/* These are the functions that actually do things. The random',
srandom',
initstate' and
setstate' functions are those from BSD Unices.
The rand' and
srand' functions are required by the ANSI standard.
We provide both interfaces to the same random number generator. /
/ Return a random long integer between 0 and RAND_MAX inclusive. */
Functions rand()
and random()
are either defined by POSIX since at least POSIX.1-2001 (and randomize()
is not standardized).
On older rand()
implementations, and on current implementations on different systems, the lower-order bits are much less random than the higher-order bits.
When available, random()
does not suffer of this issue.
In add, modern version of rand()
use the same random number generator as random()
. So rand()
may be correct, but it is not garanteed.
So, always use random()
instead of rand()
. If random()
is not available on your operating system, ask to operating system developers to provide newer standards API implementation (2001 standard is now old enough to expect any system to provide it).
It looks like you were using C-style functions, even though your question is labeled C++. Also, stdlib.h
is a header file from C standard library. There's no such functions are random()
and randomize()
in C standard library. The C standard library has rand()
and srand()
.
If you were using random()
or something like that through stdlib.h
, it must have been some non-standard library extension in Borland compiler package.
So, if you want to stick to C-style standard functions, that would be, again, rand()
and srand()
. But if you are writing in C++, you might have better (and more appropriate) options in C++ standard library.