random.h 2.62 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef LIGHTGBM_UTILS_RANDOM_H_
#define LIGHTGBM_UTILS_RANDOM_H_

#include <cstdint>

#include <random>
#include <vector>

namespace LightGBM {

/*!
* \brief A wrapper for random generator
*/
class Random {
public:
  /*!
  * \brief Constructor, with random seed
  */
Guolin Ke's avatar
Guolin Ke committed
19
  Random() {
Guolin Ke's avatar
Guolin Ke committed
20
    std::random_device rd;
Guolin Ke's avatar
Guolin Ke committed
21
22
    auto genrator = std::mt19937(rd());
    std::uniform_int_distribution<int> distribution(0, x);
Guolin Ke's avatar
Guolin Ke committed
23
    x = distribution(genrator);
Guolin Ke's avatar
Guolin Ke committed
24
25
26
27
  }
  /*!
  * \brief Constructor, with specific seed
  */
Guolin Ke's avatar
Guolin Ke committed
28
  Random(int seed) {
Guolin Ke's avatar
Guolin Ke committed
29
    x = seed;
Guolin Ke's avatar
Guolin Ke committed
30
31
  }
  /*!
Guolin Ke's avatar
Guolin Ke committed
32
33
34
35
36
37
38
39
40
41
42
  * \brief Generate random integer, int16 range. [0, 65536]
  * \param lower_bound lower bound
  * \param upper_bound upper bound
  * \return The random integer between [lower_bound, upper_bound)
  */
  inline int NextShort(int lower_bound, int upper_bound) {
    return (RandInt16()) % (upper_bound - lower_bound) + lower_bound;
  }

  /*!
  * \brief Generate random integer, int32 range
Guolin Ke's avatar
Guolin Ke committed
43
44
45
46
  * \param lower_bound lower bound
  * \param upper_bound upper bound
  * \return The random integer between [lower_bound, upper_bound)
  */
Guolin Ke's avatar
Guolin Ke committed
47
  inline int NextInt(int lower_bound, int upper_bound) {
Guolin Ke's avatar
Guolin Ke committed
48
    return (RandInt32()) % (upper_bound - lower_bound) + lower_bound;
Guolin Ke's avatar
Guolin Ke committed
49
  }
Guolin Ke's avatar
Guolin Ke committed
50

Guolin Ke's avatar
Guolin Ke committed
51
52
53
54
  /*!
  * \brief Generate random float data
  * \return The random float between [0.0, 1.0)
  */
Guolin Ke's avatar
Guolin Ke committed
55
  inline float NextFloat() {
Guolin Ke's avatar
Guolin Ke committed
56
    // get random float in [0,1)
Guolin Ke's avatar
Guolin Ke committed
57
    return static_cast<float>(RandInt16()) / (32768.0f);
Guolin Ke's avatar
Guolin Ke committed
58
59
60
61
62
63
64
  }
  /*!
  * \brief Sample K data from {0,1,...,N-1}
  * \param N
  * \param K
  * \return K Ordered sampled data from {0,1,...,N-1}
  */
65
66
  inline std::vector<int> Sample(int N, int K) {
    std::vector<int> ret;
67
    ret.reserve(K);
Guolin Ke's avatar
Guolin Ke committed
68
69
    if (K > N || K < 0) {
      return ret;
70
71
    } else if (K == N) {
      for (int i = 0; i < N; ++i) {
Guolin Ke's avatar
Guolin Ke committed
72
73
        ret.push_back(i);
      }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    } else if (K > N / 2) {
      for (int i = 0; i < N; ++i) {
        double prob = (K - ret.size()) / static_cast<double>(N - i);
        if (NextFloat() < prob) {
          ret.push_back(i);
        }
      }
    } else {
      int min_step = 1;
      int avg_step = N / K;
      int max_step = 2 * avg_step - min_step;
      int start = -1;
      for (int i = 0; i < K; ++i) {
        int step = NextShort(min_step, max_step + 1);
        start += step;
        if (start >= N) { break; }
        ret.push_back(start);
      }
Guolin Ke's avatar
Guolin Ke committed
92
93
94
95
    }
    return ret;
  }
private:
Guolin Ke's avatar
Guolin Ke committed
96
  inline int RandInt16() {
Guolin Ke's avatar
Guolin Ke committed
97
    x = (214013 * x + 2531011);
Guolin Ke's avatar
Guolin Ke committed
98
    return static_cast<int>((x >> 16) & 0x7FFF);
Guolin Ke's avatar
Guolin Ke committed
99
  }
Guolin Ke's avatar
Guolin Ke committed
100
101
102

  inline int RandInt32() {
    x = (214013 * x + 2531011);
Guolin Ke's avatar
Guolin Ke committed
103
    return static_cast<int>(x & 0x7FFFFFFF);
Guolin Ke's avatar
Guolin Ke committed
104
105
  }

106
  unsigned int x = 123456789;
Guolin Ke's avatar
Guolin Ke committed
107
108
};

Guolin Ke's avatar
Guolin Ke committed
109

Guolin Ke's avatar
Guolin Ke committed
110
111
}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
112
#endif   // LightGBM_UTILS_RANDOM_H_