Advertisement
bartekltg

Ranvec1 random float

Jul 5th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include "vectorclass.h"
  2. #include "ranvec1.h"
  3. #include <chrono>
  4. #include <random>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. constexpr int M = 20000000;
  10.  
  11. class stoper
  12. {
  13.     chrono::time_point<chrono::high_resolution_clock> a,b;
  14. public:
  15.     void start(){a = chrono::high_resolution_clock::now();}
  16.     void stop() {b = chrono::high_resolution_clock::now();}
  17.     double value()
  18.     {
  19.         chrono::duration<double> elapsed_seconds = b-a;
  20.         return elapsed_seconds.count();
  21.     }
  22.     void show()
  23.     {
  24.         cout << value()<<" ";
  25.     }
  26. };
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32.     for (int k=0;k<5;k++)
  33.     {
  34.         int seed = k;
  35.         Ranvec1 ran(3);
  36.         ran.init(seed);
  37.         stoper st;
  38.  
  39.         float sum1=0.0;
  40.  
  41.         st.start();
  42.  
  43.         Vec8f rf;
  44.         for (int i=0;i<M/8;i++){
  45.             rf = ran.random8f();
  46.             for (int j=0;j<8;j++)
  47.                 sum1+=rf[i];
  48.         }
  49.  
  50.         st.stop();
  51.         st.show();
  52.  
  53.         random_device rd;
  54.  
  55.         mt19937 gen{rd()};
  56.         uniform_real_distribution<float> dist;
  57.  
  58.         float sum2=0.0;
  59.         st.start();
  60.  
  61.         for (int i=0;i<M;i++){
  62.             sum2+= dist(gen);
  63.         }
  64.         st.stop();
  65.         st.show();
  66.  
  67.         cout<<endl<<sum1/M<<" "<<sum2/M<<endl;
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement