Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/random/srand"

From cppreference.com
< cpp‎ | numeric‎ | random
(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/sidebar}}
+
{{cpp/numeric/random/navbar}}
{{ddcl | header=cstdlib |  
+
{{ddcl|header=cstdlib|  
 
void srand( unsigned seed );
 
void srand( unsigned seed );
 
}}
 
}}
  
Initializes the built-in random number generator used to generate values for {{rlpf|rand}} with the seed value {{tt|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===
{{param list begin}}
+
{{par begin}}
{{param list item | seed | the seed value}}
+
{{par|seed|the seed value}}
{{param list end}}
+
{{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=
+
|code=
| output=
+
#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===
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/numeric/random/dcl list rand}}
+
{{dsc inc|cpp/numeric/random/dsc rand}}
{{dcl list see c | c/numeric/random/srand}}
+
{{dsc inc|cpp/numeric/random/dsc RAND_MAX}}
{{dcl list end}}
+
{{dsc inc|cpp/experimental/dsc reseed }}
 +
{{dsc see c|c/numeric/random/srand}}
 +
{{dsc end}}
  
[[es:cpp/numeric/random/srand]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[fr:cpp/numeric/random/srand]]
+
[[ja:cpp/numeric/random/srand]]
+
[[ru:cpp/numeric/random/srand]]
+
[[zh:cpp/numeric/random/srand]]
+

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

#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';
}

Possible output:

Random value on [0, 2147483647]: 1373858591

[edit] See also

generates a pseudo-random number
(function) [edit]
maximum possible value generated by std::rand
(macro constant) [edit]
reseeds the per-thread random engine
(function) [edit]
C documentation for srand