Skip to content

Instantly share code, notes, and snippets.

@kbridge
Created January 3, 2024 11:56
Show Gist options
  • Save kbridge/f870a7ea066ed87cdca2962faec10414 to your computer and use it in GitHub Desktop.
Save kbridge/f870a7ea066ed87cdca2962faec10414 to your computer and use it in GitHub Desktop.
demonstrate the usage of c++ standard library random facilities
#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