Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
|
|
Defined in header <cstdlib> | ||
int rand(); | ||
Returns a pseudo-random integral value from the range[
0,
RAND_MAX]
.
std::srand() seeds the pseudo-random number generator used byrand()
.Ifrand()
is used before any calls tostd::srand(),rand()
behaves as if it was seeded withstd::srand(1).
Each timerand()
is seeded withstd::srand(), it must produce the same sequence of values on successive calls.
Other functions in the standard library may callrand
. It is implementation-defined which functions do so.
It is implementation-defined whetherrand()
is thread-safe.
Contents |
Pseudo-random integral value between0 andRAND_MAX.
There are no guarantees as to the quality of the random sequence produced.In the past, some implementations ofrand()
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 between1 and0 between calls).
rand()
is not recommended for serious random-number generation needs.It is recommended to use C++11'srandom number generation facilities to replacerand()
.(since C++11)
The functionbounded_rand()
below is an adapted version ofDebiased Modulo (Once).
#include <cstdlib>#include <ctime>#include <initializer_list>#include <iostream> unsigned bounded_rand(unsigned range){for(unsigned x, r;;)if(x= rand(), r= x% range, x- r<=-range)return r;} int main(){std::srand(std::time({}));// use current time as seed for random generatorconstint random_value= std::rand();std::cout<<"Random value on [0, "<<RAND_MAX<<"]: "<< random_value<<'\n'; for(constunsigned sides:{2,4,6,8}){std::cout<<"Roll "<< sides<<"-sided die 8 times: ";for(int n=8; n;--n)std::cout<<1+ bounded_rand(sides)<<' '; std::cout<<'\n';}}
Possible output:
Random value on [0, 2147483647]: 948298199Roll 2-sided die 8 times: 2 2 1 2 1 1 2 2 Roll 4-sided die 8 times: 1 3 4 2 1 3 3 1 Roll 6-sided die 8 times: 3 2 1 6 6 4 4 2 Roll 8-sided die 8 times: 4 5 6 6 3 6 1 2
(C++11) | produces integer values evenly distributed across a range (class template)[edit] |
seeds pseudo-random number generator (function)[edit] | |
maximum possible value generated bystd::rand (macro constant)[edit] | |
generates a random integer in the specified range (function template)[edit] | |
C documentation forrand |