Generate random numbers using C++11 random library

后端 未结 6 780
梦毁少年i
梦毁少年i 2020-11-22 15:13

As the title suggests, I am trying to figure out a way of generating random numbers using the new C++11 library. I have tried it with this code:<

6条回答
  •  自闭症患者
    2020-11-22 15:27

    Here's something that I just wrote along those lines::

    #include 
    #include 
    #include 
    
    using namespace std;
    
    //==============================================================
    // RANDOM BACKOFF TIME
    //==============================================================
    class backoff_time_t {
      public:
        random_device                      rd;
        mt19937                            mt;
        uniform_real_distribution  dist;
    
        backoff_time_t() : rd{}, mt{rd()}, dist{0.5, 1.5} {}
    
        double rand() {
          return dist(mt);
        }
    };
    
    thread_local backoff_time_t backoff_time;
    
    
    int main(int argc, char** argv) {
       double x1 = backoff_time.rand();
       double x2 = backoff_time.rand();
       double x3 = backoff_time.rand();
       double x4 = backoff_time.rand();
       return 0;
    }
    

    ~

提交回复
热议问题