CNum 0.2.1
CPU-optimized ML library for C++
Loading...
Searching...
No Matches
RandUtils.h
Go to the documentation of this file.
1#ifndef RAND_UTILS_H
2#define RAND_UTILS_H
4#include <cstdint>
5#include <random>
6#include <map>
7#include <memory>
8#include <shared_mutex>
9#include <unordered_set>
10#include <stdexcept>
11
12#include "XoshiroCpp.hpp"
14
19namespace CNum::Utils::Rand {
26 template<typename T>
27 void generate_n_unique_rand_in_range(size_t low_bound,
28 size_t high_bound,
29 T *out,
30 size_t n,
31 uint64_t logical_id = 0);
32
41 class RandomGenerator {
42 private:
43 static uint64_t _epoch;
44 static uint64_t _seed;
45 static ::std::map<uint64_t, ::XoshiroCpp::Xoshiro256PlusPlus> _registry;
46 static ::std::map<uint64_t, uint64_t> _stream_local_epochs;
47 static ::std::mutex _seed_mtx;
48
50 RandomGenerator() = default;
51
52 public:
54 static XoshiroCpp::Xoshiro256PlusPlus &instance(uint64_t logical_id = 0);
55
58 static void set_global_seed(uint64_t new_seed);
59
61 static void reset_state();
62 };
63
64#include "CNum/Utils/RandUtils.tpp"
65};
66
67#endif
static void reset_state()
Reconstruct all thread local random generators.
static XoshiroCpp::Xoshiro256PlusPlus & instance(uint64_t logical_id=0)
Get the instance of the RandomGenerator.
static void set_global_seed(uint64_t new_seed)
Set the seed for random number generation.
Utilities for generating random numbers.
void generate_n_unique_rand_in_range(size_t low_bound, size_t high_bound, T *out, size_t n, uint64_t logical_id=0)
Generate n unique random integers.
Definition RandUtils.h:3