Advertisement
Guest User

Untitled

a guest
Feb 19th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3.  
  4. using namespace std;
  5.  
  6. template <typename Generator>
  7. double randomDoubleEngine(Generator& engine, double low_bound, double high_bound )
  8. {
  9.     if (low_bound > high_bound){
  10.         std::swap(low_bound, high_bound);
  11.     }
  12.  
  13.     return std::uniform_real_distribution<>( low_bound, high_bound )( engine );
  14. }
  15.  
  16. double doRandomDoubleInterval( double low_bound, double high_bound ){
  17.        
  18.     static bool didSeeding = false;
  19.     std::random_device rd;
  20.  
  21.     if(!didSeeding){
  22.         std::mt19937 engine(rd());
  23.         didSeeding = true;
  24.     }
  25.  
  26.     return randomDoubleEngine(engine, low_bound, high_bound );
  27. }
  28.  
  29. void doSomething(){
  30.  
  31.     double low = -1.7411500;
  32.     double high = 2.2737999;
  33.     for(int i =0; i < 20; i++){
  34.         std::cout << doRandomDoubleInterval( low, high ) << std::endl;
  35.     }
  36. }
  37.  
  38. int main(){
  39.  
  40.     for(int i = 0; i < 5; i++){
  41.         std::cout << "####################################\n";
  42.         std::cout << "# Call number: "  << i  << "\n";
  43.         doSomething();
  44.         std::cout << "####################################\n\n";
  45.     }
  46.  
  47.     return (EXIT_SUCCESS);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement