# std::rand

> C++ Atlas: `cppatlas.com/w/cpp/numeric/random/rand` | Source: https://en.cppreference.com/w/cpp/numeric/random/rand | Domain: Numerics | Standards: C++11

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

## Official Specification Summary (cppreference)

```cpp
// 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 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.

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

### Example

The function `bounded_rand()` below is an adapted version of Debiased Modulo (Once).

```cpp
#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 generator
    const int random_value = std::rand();
    std::cout << "Random value on [0, " << RAND_MAX << "]: " << random_value << '\n';

    for (const unsigned 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:

```text
Random value on [0, 2147483647]: 948298199
Roll 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
```

### See also

- [`uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) (C++11): produces integer values evenly distributed across a range (class template)
- [`srand`](https://en.cppreference.com/w/cpp/numeric/random/srand): seeds pseudo-random number generator (function)
- [`RAND_MAX`](https://en.cppreference.com/w/cpp/numeric/random/RAND_MAX): maximum possible value generated by `std::rand` (macro constant)
- [`randint`](https://en.cppreference.com/w/cpp/experimental/randint): generates a random integer in the specified range (function template)
- [C documentation for `rand`](https://en.cppreference.com/w/c/numeric/random/rand)

---
Attribution: content from https://en.cppreference.com/w/cpp/numeric/random/rand (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.