Difference between revisions of "cpp/numeric/random/rand"
m (r2.7.3) (Robot: Adding de:cpp/numeric/random/rand) |
(See discussion page.) |
||
Line 5: | Line 5: | ||
}} | }} | ||
− | Returns a | + | Returns a pseudo-random integral value between {{c|0}} and {{rlpt|RAND_MAX}} (0 and RAND_MAX included). |
− | {{rlpf|srand}} | + | {{rlpf|srand}} seeds the pseudo-random number generator used by {{tt|rand()}}. |
+ | If {{tt|rand()}} is used before any calls to {{tt|srand()}}, {{tt|rand()}} behaves as if it was seeded with {{tt|srand(1)}}. | ||
+ | Each time {{tt|rand()}} is seeded with {{tt|srand()}}, it must produce the same sequence of values. | ||
===Parameters=== | ===Parameters=== | ||
Line 14: | Line 16: | ||
===Return value=== | ===Return value=== | ||
Pseudo-random integral value between {{c|0}} and {{rlpt|RAND_MAX}}. | Pseudo-random integral value between {{c|0}} and {{rlpt|RAND_MAX}}. | ||
+ | |||
+ | ===Notes=== | ||
+ | There are no guarantees as to the quality of the random sequence produced. | ||
+ | In the past, some implementations of {{tt|rand()}} have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between {{tt|1}} and {{tt|0}} between calls). | ||
+ | {{tt|rand()}} is not recommended for serious random-number generation needs, like cryptography. | ||
+ | |||
+ | {{tt|rand()}} is not required to be thread-safe. | ||
===Example=== | ===Example=== | ||
Line 26: | Line 35: | ||
{ | { | ||
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 | ||
− | int | + | int random_variable = std::rand(); |
− | std::cout << " | + | std::cout << "Random value on [0 " << RAND_MAX << "]: " |
− | << | + | << random_variable << '\n'; |
} | } | ||
| output= | | output= | ||
− | + | Random value on [0 2147483647]: 1373858591 | |
}} | }} | ||
Revision as of 11:29, 11 November 2012
Defined in header <cstdlib>
|
||
int rand(); |
||
Returns a pseudo-random integral value between 0 and RAND_MAX
(0 and RAND_MAX included).
srand()
seeds the pseudo-random number generator used by rand()
.
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 srand()
, it must produce the same sequence of values.
Contents |
Parameters
(none)
Return value
Pseudo-random integral value between 0 and RAND_MAX
.
Notes
There are no guarantees as to the quality of the random sequence produced.
In the past, some implementations of rand()
have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1
and 0
between calls).
rand()
is not recommended for serious random-number generation needs, like cryptography.
rand()
is not required to be thread-safe.
Example
#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
C documentation for rand
|