-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move util_random.hpp > util/random.hpp
- Loading branch information
1 parent
8ab10ef
commit 2ba4665
Showing
7 changed files
with
182 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2019, QuantStack and Mamba Contributors | ||
// | ||
// Distributed under the terms of the BSD 3-Clause License. | ||
// | ||
// The full license is in the file LICENSE, distributed with this software. | ||
|
||
#ifndef MAMBA_UTIL_RANDOM_HPP | ||
#define MAMBA_UTIL_RANDOM_HPP | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <cstring> | ||
#include <limits> | ||
#include <random> | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace mamba::util | ||
{ | ||
using default_random_generator = std::mt19937; | ||
|
||
template <typename Generator> | ||
[[nodiscard]] auto random_generator() -> Generator; | ||
|
||
[[nodiscard]] auto random_generator() -> default_random_generator; | ||
|
||
template <typename Generator> | ||
auto local_random_generator() -> Generator&; | ||
|
||
auto local_random_generator() -> default_random_generator&; | ||
|
||
template <typename T = int, typename Generator = default_random_generator> | ||
auto random_int(T min, T max, Generator& generator = local_random_generator()) -> T; | ||
|
||
template <typename Generator> | ||
auto generate_random_alphanumeric_string(std::size_t len, Generator& generator) -> std::string; | ||
|
||
auto generate_random_alphanumeric_string(std::size_t len) -> std::string; | ||
|
||
/******************** | ||
* Implementation * | ||
********************/ | ||
|
||
template <typename Generator> | ||
auto random_generator() -> Generator | ||
{ | ||
using std::begin; | ||
using std::end; | ||
constexpr auto seed_bits = sizeof(typename Generator::result_type) * Generator::state_size; | ||
constexpr auto seed_len = seed_bits / std::numeric_limits<std::seed_seq::result_type>::digits; | ||
auto seed = std::array<std::seed_seq::result_type, seed_len>{}; | ||
auto dev = std::random_device{}; | ||
std::generate_n(begin(seed), seed_len, std::ref(dev)); | ||
auto seed_seq = std::seed_seq(begin(seed), end(seed)); | ||
return Generator{ seed_seq }; | ||
} | ||
|
||
template <typename Generator> | ||
auto local_random_generator() -> Generator& | ||
{ | ||
thread_local auto rng = random_generator<Generator>(); | ||
return rng; | ||
} | ||
|
||
template <typename T, typename Generator> | ||
auto random_int(T min, T max, Generator& generator) -> T | ||
{ | ||
return std::uniform_int_distribution<T>{ min, max }(generator); | ||
} | ||
|
||
template <typename Generator> | ||
auto generate_random_alphanumeric_string(std::size_t len, Generator& generator) -> std::string | ||
{ | ||
static constexpr auto chars = std::string_view{ | ||
"0123456789" | ||
"abcdefghijklmnopqrstuvwxyz", | ||
}; | ||
auto dist = std::uniform_int_distribution{ {}, chars.size() - 1 }; | ||
auto result = std::string(len, '\0'); | ||
std::generate_n(begin(result), len, [&]() { return chars[dist(generator)]; }); | ||
return result; | ||
} | ||
} | ||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2019, QuantStack and Mamba Contributors | ||
// | ||
// Distributed under the terms of the BSD 3-Clause License. | ||
// | ||
// The full license is in the file LICENSE, distributed with this software. | ||
|
||
#include "mamba/util/random.hpp" | ||
|
||
namespace mamba ::util | ||
{ | ||
auto random_generator() -> default_random_generator | ||
{ | ||
return random_generator<default_random_generator>(); | ||
} | ||
|
||
auto local_random_generator() -> default_random_generator& | ||
{ | ||
return local_random_generator<default_random_generator>(); | ||
} | ||
|
||
auto generate_random_alphanumeric_string(std::size_t len) -> std::string | ||
{ | ||
return generate_random_alphanumeric_string(len, local_random_generator()); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2023, QuantStack and Mamba Contributors | ||
// | ||
// Distributed under the terms of the BSD 3-Clause License. | ||
// | ||
// The full license is in the file LICENSE, distributed with this software. | ||
|
||
#include <thread> | ||
|
||
#include <doctest/doctest.h> | ||
|
||
#include "mamba/util/random.hpp" | ||
#include "mamba/util/string.hpp" | ||
|
||
using namespace mamba::util; | ||
|
||
TEST_SUITE("util::random") | ||
{ | ||
TEST_CASE("local_random_generator") | ||
{ | ||
auto same_thread_checks = [] | ||
{ | ||
auto& a = local_random_generator(); | ||
auto& b = local_random_generator(); | ||
CHECK_EQ(&a, &b); | ||
|
||
auto& c = local_random_generator<std::mt19937>(); | ||
CHECK_EQ(&a, &c); | ||
|
||
auto& d = local_random_generator<std::mt19937_64>(); | ||
CHECK_NE(static_cast<void*>(&a), static_cast<void*>(&d)); | ||
|
||
return &a; | ||
}; | ||
void* pointer_to_this_thread_rng = same_thread_checks(); | ||
|
||
void* pointer_to_another_thread_rng = nullptr; | ||
std::thread another_thread{ [&] { pointer_to_another_thread_rng = same_thread_checks(); } }; | ||
another_thread.join(); | ||
|
||
CHECK_NE(pointer_to_this_thread_rng, pointer_to_another_thread_rng); | ||
} | ||
|
||
TEST_CASE("value_in_range") | ||
{ | ||
constexpr int arbitrary_min = -20; | ||
constexpr int arbitrary_max = 20; | ||
constexpr std::size_t attempts = 2000; | ||
for (std::size_t i = 0; i < attempts; ++i) | ||
{ | ||
const int value = random_int(arbitrary_min, arbitrary_max); | ||
CHECK_GE(value, arbitrary_min); | ||
CHECK_LE(value, arbitrary_max); | ||
} | ||
} | ||
|
||
TEST_CASE("random_alphanumeric_string") | ||
{ | ||
constexpr std::size_t attempts = 200; | ||
for (std::size_t i = 0; i < attempts; ++i) | ||
{ | ||
const auto value = generate_random_alphanumeric_string(i); | ||
CHECK_EQ(value.size(), i); | ||
CHECK(std::all_of( | ||
value.cbegin(), | ||
value.cend(), | ||
[](char c) { return is_digit(c) || is_alpha(c); } | ||
)); | ||
} | ||
} | ||
} |