! * \brief A wrapper for random generator */
| 16 | * \brief A wrapper for random generator |
| 17 | */ |
| 18 | class Random { |
| 19 | public: |
| 20 | /*! |
| 21 | * \brief Constructor, with random seed |
| 22 | */ |
| 23 | Random() { |
| 24 | std::random_device rd; |
| 25 | auto genrator = std::mt19937(rd()); |
| 26 | std::uniform_int_distribution<int> distribution(0, x); |
| 27 | x = distribution(genrator); |
| 28 | } |
| 29 | /*! |
| 30 | * \brief Constructor, with specific seed |
| 31 | */ |
| 32 | explicit Random(int seed) { |
| 33 | x = seed; |
| 34 | } |
| 35 | /*! |
| 36 | * \brief Generate random integer, int16 range. [0, 65536] |
| 37 | * \param lower_bound lower bound |
| 38 | * \param upper_bound upper bound |
| 39 | * \return The random integer between [lower_bound, upper_bound) |
| 40 | */ |
| 41 | inline int NextShort(int lower_bound, int upper_bound) { |
| 42 | return (RandInt16()) % (upper_bound - lower_bound) + lower_bound; |
| 43 | } |
| 44 | |
| 45 | /*! |
| 46 | * \brief Generate random integer, int32 range |
| 47 | * \param lower_bound lower bound |
| 48 | * \param upper_bound upper bound |
| 49 | * \return The random integer between [lower_bound, upper_bound) |
| 50 | */ |
| 51 | inline int NextInt(int lower_bound, int upper_bound) { |
| 52 | return (RandInt32()) % (upper_bound - lower_bound) + lower_bound; |
| 53 | } |
| 54 | |
| 55 | /*! |
| 56 | * \brief Generate random float data |
| 57 | * \return The random float between [0.0, 1.0) |
| 58 | */ |
| 59 | inline float NextFloat() { |
| 60 | // get random float in [0,1) |
| 61 | return static_cast<float>(RandInt16()) / (32768.0f); |
| 62 | } |
| 63 | /*! |
| 64 | * \brief Sample K data from {0,1,...,N-1} |
| 65 | * \param N |
| 66 | * \param K |
| 67 | * \return K Ordered sampled data from {0,1,...,N-1} |
| 68 | */ |
| 69 | inline std::vector<int> Sample(int N, int K) { |
| 70 | std::vector<int> ret; |
| 71 | ret.reserve(K); |
| 72 | if (K > N || K <= 0) { |
| 73 | return ret; |
| 74 | } else if (K == N) { |
| 75 | for (int i = 0; i < N; ++i) { |
no outgoing calls
no test coverage detected