Created
January 3, 2024 11:56
-
-
Save kbridge/f870a7ea066ed87cdca2962faec10414 to your computer and use it in GitHub Desktop.
demonstrate the usage of c++ standard library random facilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <random> | |
#include <string> | |
std::string get_random_string(size_t n) | |
{ | |
std::random_device device; | |
std::default_random_engine::result_type seed = device(); | |
std::default_random_engine engine(seed); | |
std::uniform_int_distribution<int> distribution('a', 'z'); | |
std::string result(n, '\0'); | |
for (size_t i = 0; i < n; ++i) | |
result[i] = static_cast<char>(distribution(engine)); | |
return result; | |
} | |
int main() | |
{ | |
std::cout << get_random_string(10) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment