Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | random
m (r2.7.3) (Robot: Adding es, fr, it, ja, pt, ru, zh)
(See also: +see c)
Line 37: Line 37:
 
{{dcl list begin}}
 
{{dcl list begin}}
 
{{dcl list template | cpp/numeric/random/dcl list srand}}
 
{{dcl list template | cpp/numeric/random/dcl list srand}}
 +
{{dcl list see c | c/numeric/random/rand}}
 
{{dcl list end}}
 
{{dcl list end}}
  

Revision as of 11:07, 15 June 2012

Template:cpp/numeric/random/sidebar

Defined in header <cstdlib>
int rand();

Returns a uniformly distributed pseudo-random integral value between 0 and RAND_MAX (0 and RAND_MAX included).

srand() should be called before any calls to rand() to initialize the random number generator.

Contents

Parameters

(none)

Return value

Pseudo-random integral value between 0 and RAND_MAX.

Example

#include <cstdlib>
#include <iostream>
#include <ctime>
 
int main() 
{
    std::srand(time(0)); //use current time as seed for random generator
    int uniform_random_variable = std::rand();
    std::cout << "Uniform random value on [0 " << RAND_MAX << "]: " 
              << uniform_random_variable << '\n';
}

Possible output:

Uniform random value on [0 2147483647]: 1373858591

See also

Template:cpp/numeric/random/dcl list srand