Difference between revisions of "cpp/numeric/random/srand"
(use {{lc}}) |
m (→Example: -intermediate variable) |
||
(9 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|srand}} | {{cpp/title|srand}} | ||
{{cpp/numeric/random/navbar}} | {{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|rand()}} with the value {{tt|seed}}. | + | Seeds the pseudo-random number generator used by {{lc|std::rand()}} with the value {{tt|seed}}. |
− | If {{ | + | 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 {{ | + | |
+ | 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. | {{tt|srand()}} is not guaranteed to be thread-safe. | ||
Line 14: | Line 15: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | seed | the seed value}} | + | {{par|seed|the seed value}} |
{{par end}} | {{par end}} | ||
Line 21: | Line 22: | ||
===Notes=== | ===Notes=== | ||
− | Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to {{tt|rand()}}, | + | 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. | 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|time(0)}} as the seed. | + | Standard practice is to use the result of a call to {{c|std::time(0)}} as the seed. |
− | However, {{ | + | 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 {{ | + | 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 <cstdlib> | ||
− | |||
#include <ctime> | #include <ctime> | ||
+ | #include <iostream> | ||
int main() | int main() | ||
{ | { | ||
− | std::srand(std::time(0)); //use current time as seed for random generator | + | std::srand(std::time(0)); // use current time as seed for random generator |
− | + | std::cout << "Random value on [0, " << RAND_MAX << "]: " << std::rand() << '\n'; | |
− | std::cout << "Random value on [0 " << RAND_MAX << "]: " | + | |
− | + | ||
} | } | ||
− | + | |p=true | |
− | Random value on [0 2147483647]: 1373858591 | + | |output= |
+ | Random value on [0, 2147483647]: 1373858591 | ||
}} | }} | ||
Line 50: | Line 49: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/random/dsc rand}} | + | {{dsc inc|cpp/numeric/random/dsc rand}} |
− | {{dsc see c | c/numeric/random/srand}} | + | {{dsc inc|cpp/numeric/random/dsc RAND_MAX}} |
+ | {{dsc inc|cpp/experimental/dsc reseed }} | ||
+ | {{dsc see c|c/numeric/random/srand}} | ||
{{dsc end}} | {{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
|