std::rand(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::rand β€” bestie's very first random number era ✨ (yes the NAME section just says std::rand - std::rand, we're keeping it real, no notes πŸ’€)

This little icon lives in the C++ Standard Library (the manual name, folding it in like a cute lil cardigan) πŸŽ€


✨ SYNOPSIS ✨

Defined in header <cstdlib> πŸ“

int rand();

So basically: it hands you a pseudo-random integral value from the range [0, RAND_MAX]. That's the whole vibe πŸ¦‹

Now the seeding lore, because this part matters, no cap:

std::srand() seeds the pseudo-random number generator that rand() uses. And if you call rand() before any calls to std::srand()? It behaves as if it was seeded with std::srand(1). Like it just defaults to the same starting fit every time πŸ™ˆ

Each time rand() is seeded with std::srand(), it must produce the same sequence of values on successive calls. Same seed, same sequence β€” that's the contract, fr fr 🌸

Other functions in the standard library may call rand. It is implementation-defined which functions do so. So like, you're not the only one in the group chat calling it πŸ‘€

It is implementation-defined whether rand() is thread-safe. Not gonna lie, that's a little scary but it is what it is πŸ’€


🫢 PARAMETERS 🫢

(none) β€” takes literally nothing, zero arguments, main character who needs no input πŸ’…


πŸ‘‘ RETURN VALUE πŸ‘‘

A pseudo-random integral value between 0 and RAND_MAX. That's it. That's the gift 🎁


🌈 NOTES 🌈

Okay so real talk, and I need you to hear me on this one:

There are no guarantees as to the quality of the random sequence produced. None. Zero. The bar is on the floor 😭

In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced. And bestie, one well-known example: the low-order bit simply alternated between 1 and 0 between calls. Like literally 1, 0, 1, 0, that's not random that's a metronome πŸ’€πŸ’€

🚨 rand() is not recommended for serious random-number generation needs. Periodt.

It is recommended to use C++11's random number generation facilities to replace rand(). 🧚✨

(since C++11)


πŸ”₯ EXAMPLE πŸ”₯

// Run this code

#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';
    }
}

Obsessed with this example honestly β€” it seeds with std::time(nullptr) (current time as seed, iconic), prints the value on [0, RAND_MAX], then rolls dice of 2, 4, 6 and 8 sides, 8 times each 🎲 And clock the comment: 1 + rand() % sides is biased, so they do the division dance instead. Read the comment, bestie, iykyk πŸ’œ


⭐ 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

(That's possible output, not guaranteed output β€” remember the notes section, bestie 😭)


πŸ¦‹ SEE ALSO πŸ¦‹

  • uniform_int_distribution β€” produces integer values evenly distributed across a range (C++11) β€” (class template) πŸ‘‘
  • srand β€” seeds pseudo-random number generator β€” (function) 🌱
  • RAND_MAX β€” maximum possible value generated by std::rand β€” (macro constant) πŸ”
  • randint β€” generates a random integer in the specified range β€” (function template) πŸŽ€
  • C documentation for rand β€” the crossover episode, go read it πŸ’–

Okay that's the whole page, slay πŸ’…βœ¨ go forth and seed responsibly β€” and remember what we said about serious random-number needs (don't use this one, use the C++11 facilities, I'm begging πŸ™).

Source/version and date, as promised in the cute sign-off: this page came from http://cppreference.com, version/date 2024.06.10, and yes, it's std::rand(3) β€” that's the footer, folded into a sign-off instead of copy-pasted, you're welcome 🫢🌸

bye bestie, happy rolling πŸŽ²πŸ’–

πŸ“¦ where this page comes from