jack06215

[tools] random from range

Jul 8th, 2020 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. template <typename T> T randomFrom(const T min, const T max)
  2. {
  3.     static std::random_device rdev;
  4.     static std::default_random_engine re(rdev());
  5.  
  6.     // Create a compile-time conditional 'dist_type' checking on whether the 'value' is a 'real_dist' or 'int_dist'
  7.     typedef typename std::conditional<
  8.         std::is_floating_point<T>::value,
  9.         std::uniform_real_distribution<T>,
  10.         std::uniform_int_distribution<T> > ::type dist_type;
  11.  
  12.     dist_type uni(min, max);
  13.     return static_cast<T>(uni(re));
  14. }
Add Comment
Please, Sign In to add comment