Skip to content

Instantly share code, notes, and snippets.

@clalancette
Created December 5, 2019 19:49
Show Gist options
  • Save clalancette/fedda4179d8956a13bb4e782f8c09dbc to your computer and use it in GitHub Desktop.
Save clalancette/fedda4179d8956a13bb4e782f8c09dbc to your computer and use it in GitHub Desktop.
Alternative implementation of get_unique_node_name for https://github.com/ros2/geometry2/pull/182
std::string get_unique_node_name()
{
const static std::string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static std::random_device rd;
static std::minstd_rand g{rd()};
// The uniform_int_distribution takes a closed interval of [a, b]; since that
// would include the \0, we remove one from our string length.
static std::uniform_int_distribution<std::string::size_type> pick(0, chars.length() - 1);
std::string s("static_transform_publisher_");
size_t orig_length = s.length();
s.resize(orig_length + 16);
for (size_t i = orig_length; i < s.length(); ++i) {
s[i] = chars[pick(g)];
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment