Advertisement
Radfler

::random (from -1.0 to 1.0)

Feb 17th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.33 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <limits>
  4.  
  5. double random() {
  6.  
  7.     constexpr double EPSILON = std::numeric_limits<double>::epsilon();
  8.  
  9.     const double RANDOM = std::rand();
  10.  
  11.     const double STAGE_1 = ((2.0 * RANDOM) / RAND_MAX) - 1.0;
  12.     const double STAGE_2 = STAGE_1 - std::copysign(EPSILON, STAGE_1);
  13.  
  14.     return STAGE_2;
  15.  
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement