Difference between revisions of "cpp/numeric/random/srand"
(→See also: +see c) |
m (→Example: -intermediate variable) |
||
(16 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|srand}} | {{cpp/title|srand}} | ||
− | {{cpp/numeric/random/ | + | {{cpp/numeric/random/navbar}} |
− | {{ddcl | header=cstdlib | | + | {{ddcl|header=cstdlib| |
void srand( unsigned seed ); | void srand( unsigned seed ); | ||
}} | }} | ||
− | + | Seeds the pseudo-random number generator used by {{lc|std::rand()}} with the value {{tt|seed}}. | |
+ | |||
+ | If {{lc|std::rand()}} is used before any calls to {{tt|srand()}}, {{lc|std::rand()}} behaves as if it was seeded with {{c|srand(1)}}. | ||
+ | |||
+ | Each time {{lc|std::rand()}} is seeded with the same {{tt|seed}}, it must produce the same sequence of values. | ||
+ | |||
+ | {{tt|srand()}} is not guaranteed to be thread-safe. | ||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|seed|the seed value}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
(none) | (none) | ||
+ | |||
+ | ===Notes=== | ||
+ | Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to {{tt|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 {{c|std::time(0)}} as the seed. | ||
+ | However, {{lc|std::time}} returns a {{c|std::time_t}} value, and {{c|std::time_t}} is not guaranteed to be an integral type. | ||
+ | In practice, though, every major implementation defines {{c|std::time_t}} to be an integral type, and this is also what POSIX requires. | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | #include <cstdlib> | |
+ | #include <ctime> | ||
+ | #include <iostream> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::srand(std::time(0)); // use current time as seed for random generator | ||
+ | std::cout << "Random value on [0, " << RAND_MAX << "]: " << std::rand() << '\n'; | ||
+ | } | ||
+ | |p=true | ||
+ | |output= | ||
+ | Random value on [0, 2147483647]: 1373858591 | ||
}} | }} | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/numeric/random/dsc rand}} |
− | {{ | + | {{dsc inc|cpp/numeric/random/dsc RAND_MAX}} |
− | {{ | + | {{dsc inc|cpp/experimental/dsc reseed }} |
+ | {{dsc see c|c/numeric/random/srand}} | ||
+ | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 04:22, 6 May 2023
Defined in header <cstdlib>
|
||
void srand( unsigned seed ); |
||
Seeds the pseudo-random number generator used by std::rand() with the value seed
.
If std::rand() is used before any calls to srand()
, std::rand() behaves as if it was seeded with srand(1).
Each time std::rand() is seeded with the same seed
, it must produce the same sequence of values.
srand()
is not guaranteed to be thread-safe.
Contents |
[edit] Parameters
seed | - | the seed value |
[edit] Return value
(none)
[edit] 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 std::time(0) as the seed. However, std::time returns a std::time_t value, and std::time_t is not guaranteed to be an integral type. In practice, though, every major implementation defines std::time_t to be an integral type, and this is also what POSIX requires.
[edit] Example
Possible output:
Random value on [0, 2147483647]: 1373858591
[edit] See also
generates a pseudo-random number (function) | |
maximum possible value generated by std::rand (macro constant) | |
reseeds the per-thread random engine (function) | |
C documentation for srand
|