std::srand (3) - Linux Manuals
std::srand: std::srand
Command to display std::srand
manual in Linux: $ man 3 std::srand
NAME
std::srand - std::srand
Synopsis
Defined in header <cstdlib>
void srand( unsigned seed );
Seeds the pseudo-random number generator used by std::rand() with the value seed.
If rand() is used before any calls to srand(), rand() behaves as if it was seeded with srand(1).
Each time rand() is seeded with the same seed, it must produce the same sequence of values.
srand() is not guaranteed to be thread-safe.
Parameters
seed - the seed value
Return value
(none)
Notes
Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to rand(), at the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.
Standard practice is to use the result of a call to time(0) as the seed. However, time() returns a time_t value, and time_t is not guaranteed to be an integral type. In practice, though, every major implementation defines time_t to be an integral type, and this is also what POSIX requires.
Example
// Run this code
#include <cstdlib>
#include <iostream>
#include <ctime>
int main()
{
std::srand(std::time(0)); //use current time as seed for random generator
int random_variable = std::rand();
std::cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}
Possible output:
Random value on [0 2147483647]: 1373858591
See also
generates a pseudo-random number
rand (function)
maximum possible value generated by std::rand
RAND_MAX (macro constant)
reseeds the per-thread random engine
reseed (function)
Pages related to std::srand
- std::sample (3) - std::sample
- std::scalbn,std::scalbnf,std::scalbnl,std::scalbln,std::scalblnf,std::scalblnl (3) - std::scalbn,std::scalbnf,std::scalbnl,std::scalbln,std::scalblnf,std::scalblnl
- std::scanf,std::fscanf,std::sscanf (3) - std::scanf,std::fscanf,std::sscanf
- std::scoped_allocator_adaptor (3) - std::scoped_allocator_adaptor
- std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::allocate (3) - std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::allocate
- std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::construct (3) - std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::construct
- std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::deallocate (3) - std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::deallocate
- std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::destroy (3) - std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::destroy
- std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::inner_allocator (3) - std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::inner_allocator
- std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::max_size (3) - std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::max_size