Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | random
(time => std::time)
m (Example: Added more dices.)
 
(35 intermediate revisions by 17 users not shown)
Line 1: Line 1:
 
{{cpp/title|rand}}
 
{{cpp/title|rand}}
 
{{cpp/numeric/random/navbar}}
 
{{cpp/numeric/random/navbar}}
{{ddcl | header=cstdlib |  
+
{{ddcl|header=cstdlib|  
 
int rand();
 
int rand();
 
}}
 
}}
  
Returns a uniformly distributed pseudo-random integral value between {{c|0}} and {{rlpt|RAND_MAX}} (0 and RAND_MAX included).
+
Returns a pseudo-random integral value from the range {{closed range|0|RAND_MAX|]}}.
  
{{rlpf|srand}} should be called before any calls to {{tt|rand()}} to initialize the random number generator.
+
{{lc|std::srand()}} seeds the pseudo-random number generator used by {{tt|rand()}}.
 +
If {{tt|rand()}} is used before any calls to {{lc|std::srand()}}, {{tt|rand()}} behaves as if it was seeded with {{c|std::srand(1)}}.
 +
 
 +
Each time {{tt|rand()}} is seeded with {{lc|std::srand()}}, it must produce the same sequence of values on successive calls.
 +
 
 +
Other functions in the standard library may call {{tt|rand}}. It is implementation-defined which functions do so.
 +
 
 +
It is implementation-defined whether {{tt|rand()}} is thread-safe.
  
 
===Parameters===
 
===Parameters===
Line 13: Line 20:
  
 
===Return value===
 
===Return value===
Pseudo-random integral value between {{c|0}} and {{rlpt|RAND_MAX}}.
+
Pseudo-random integral value between {{c|0}} and {{lc|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 {{c|1}} and {{c|0}} between calls).
 +
 
 +
{{tt|rand()}} is not recommended for serious random-number generation needs. {{rev inl|since=c++11|It is recommended to use C++11's [[cpp/numeric/random|random number generation]] facilities to replace {{tt|rand()}}.}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| p=true
+
|code=
| code=
+
 
#include <cstdlib>
 
#include <cstdlib>
#include <iostream>
 
 
#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(nullptr)); // use current time as seed for random generator
     int uniform_random_variable = std::rand();
+
     int random_value = std::rand();
     std::cout << "Uniform random value on [0 " << RAND_MAX << "]: "  
+
     std::cout << "Random value on [0, " << RAND_MAX << "]: " << random_value << '\n';
              << uniform_random_variable << '\n';
+
 
 +
    for (const int times = 8; const int sides : {2, 4, 6, 8})
 +
    {
 +
        std::cout << "Roll " << sides << "-sided dice " << times << " times: ";
 +
        for (int n = 0; n != times; ++n)
 +
        {
 +
            int x = sides + 1;
 +
            while (x > sides)
 +
                x = 1 + std::rand() / ((RAND_MAX + 1u) / sides);
 +
                    // Note: 1 + rand() % sides is biased
 +
            std::cout << x << ' ';
 +
        }
 +
        std::cout << '\n';
 +
    }
 
}
 
}
| output=
+
|p=true
Uniform random value on [0 2147483647]: 1373858591
+
|output=
 +
Random value on [0, 2147483647]: 948298199
 +
Roll 2-sided dice 8 times: 2 2 1 2 1 1 2 2
 +
Roll 4-sided dice 8 times: 1 3 4 2 1 3 3 1
 +
Roll 6-sided dice 8 times: 3 2 1 6 6 4 4 2
 +
Roll 8-sided dice 8 times: 4 5 6 6 3 6 1 2
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/numeric/random/dcl list srand}}
+
{{dsc inc|cpp/numeric/random/dsc uniform_int_distribution}}
{{dcl list see c | c/numeric/random/rand}}
+
{{dsc inc|cpp/numeric/random/dsc srand}}
{{dcl list end}}
+
{{dsc inc|cpp/numeric/random/dsc RAND_MAX}}
 +
{{dsc inc|cpp/experimental/dsc randint}}
 +
{{dsc see c|c/numeric/random/rand}}
 +
{{dsc end}}
  
[[es:cpp/numeric/random/rand]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[fr:cpp/numeric/random/rand]]
+
[[it:cpp/numeric/random/rand]]
+
[[ja:cpp/numeric/random/rand]]
+
[[pt:cpp/numeric/random/rand]]
+
[[ru:cpp/numeric/random/rand]]
+
[[zh:cpp/numeric/random/rand]]
+

Latest revision as of 03:43, 24 April 2024

 
 
 
 
Defined in header <cstdlib>
int rand();

Returns a pseudo-random integral value from the range [0RAND_MAX].

std::srand() seeds the pseudo-random number generator used by rand(). If rand() is used before any calls to std::srand(), rand() behaves as if it was seeded with std::srand(1).

Each time rand() is seeded with std::srand(), it must produce the same sequence of values on successive calls.

Other functions in the standard library may call rand. It is implementation-defined which functions do so.

It is implementation-defined whether rand() is thread-safe.

Contents

[edit] Parameters

(none)

[edit] Return value

Pseudo-random integral value between 0 and RAND_MAX.

[edit] 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. It is recommended to use C++11's random number generation facilities to replace rand().(since C++11)

[edit] Example

#include <cstdlib>
#include <ctime>
#include <iostream>
 
int main() 
{
    std::srand(std::time(nullptr)); // use current time as seed for random generator
    int random_value = std::rand();
    std::cout << "Random value on [0, " << RAND_MAX << "]: " << random_value << '\n';
 
    for (const int times = 8; const int sides : {2, 4, 6, 8})
    {
        std::cout << "Roll " << sides << "-sided dice " << times << " times: ";
        for (int n = 0; n != times; ++n)
        {
            int x = sides + 1;
            while (x > sides) 
                x = 1 + std::rand() / ((RAND_MAX + 1u) / sides); 
                    // Note: 1 + rand() % sides is biased
            std::cout << x << ' ';
        }
        std::cout << '\n';
    }
}

Possible output:

Random value on [0, 2147483647]: 948298199
Roll 2-sided dice 8 times: 2 2 1 2 1 1 2 2 
Roll 4-sided dice 8 times: 1 3 4 2 1 3 3 1 
Roll 6-sided dice 8 times: 3 2 1 6 6 4 4 2 
Roll 8-sided dice 8 times: 4 5 6 6 3 6 1 2

[edit] See also

produces integer values evenly distributed across a range
(class template) [edit]
seeds pseudo-random number generator
(function) [edit]
maximum possible value generated by std::rand
(macro constant) [edit]
generates a random integer in the specified range
(function template) [edit]