std::rand(3)
π 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 bystd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms