Once, my teacher taught me to use randomize()
and random()
function for generating pseudorandom numbers in C++ Builder. Now I prefer working in VS 2012, but when I tried to use these functions there it says that "identifier not found", even when I added #include <stdlib.h>
. After some time of Googling I found that there are also rand()
and srand()
functions. What is the difference between them and which is it better to use?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Functions
rand()
andrandom()
are either defined by POSIX since at least POSIX.1-2001 (andrandomize()
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 asrandom()
. Sorand()
may be correct, but it is not garanteed.So, always use
random()
instead ofrand()
. Ifrandom()
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).srand()
is the C Standard library implementation for seeding the (pseudo) random number generator.rand()
is the (pseudo) random number generator in the C Standard Library.C++ has implemented a newer (pseudo) random number generator in the
<random>
header file, which has a variety of different engines to use: http://en.cppreference.com/w/cpp/numeric/randomAlthough 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. Therand' 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. */randomize()
andrandom()
are not part of the standard library. Perhaps your teacher wrote functions with these names for use in your class, or maybe you really meanrandom()
andsrandom()
which are part of POSIX and not available on Windows.rand()
andsrand()
are part of the standard library and will be provided by any standard conforming implementation of C++.You should avoid
rand()
andsrand()
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 Harmfulrand()
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>
:I am not familiar with
randomize()
andrandom()
but they are not part of the standard library. You should avoid usingrand()
this video explains why using rand() is considered harmful.You should be using the random header introduced in C++11, here is example using both std::uniform_real_distribution and std::uniform_int_distribution:
This is a work-around using random numbers form outside of c++.
This is the original program in C, copied from "http://www.programmingsimplified.com/" This program does not run because the " temp1 = 1 + random ( 588 );" "temp2 = 1 + random ( 380 );" statements do not work. 'random' is not a function of graphics.h, conio.h, or stdlib.h Nor, does it work if you include random.h. Below this listing is a work-around for the random function.
The random numbers are generated with a simple MS Excel macro, listed here:
This generates 2 columns of random numbers. Each column is copied and pasted into its own *.txt file , i.e. rnd1.txt and rnd2.txt and placed in a directory where they can be accessed by the c++ program that follows. Substitute the "c:\PATH\rnd1.txt" and "C:\PATH\rnd2.txt" with the correct path.
This program will run for about 40 seconds and then terminate.