Pol  Revision:4b29d2b
random.cpp
Go to the documentation of this file.
1 
9 #include <chrono>
10 #include <random>
11 
12 #include "random.h"
13 
14 namespace Pol
15 {
16 namespace Clib
17 {
18 namespace
19 {
20 static std::mt19937 generator(
21  static_cast<unsigned>( std::chrono::system_clock::now().time_since_epoch().count() ) );
22 }
23 
24 // returns [0,f]
25 double random_double( double f )
26 {
27  if ( f <= 0 )
28  return 0;
29  std::uniform_real_distribution<double> distribution( 0, f );
30  return distribution( generator );
31 }
32 
33 // returns [0,i]
34 int random_int( int i )
35 {
36  if ( i == 0 )
37  return i;
38  std::uniform_int_distribution<int> distribution( 0, i );
39  return distribution( generator );
40 }
41 
42 // returns [minI,maxI]
43 int random_int_range( int minI, int maxI )
44 {
45  if ( maxI < minI )
46  std::swap( maxI, minI );
47  if ( minI == maxI )
48  return minI;
49  std::uniform_int_distribution<int> distribution( minI, maxI );
50  return distribution( generator );
51 }
52 }
53 }
int random_int(int i)
Definition: random.cpp:34
int random_int_range(int minI, int maxI)
Definition: random.cpp:43
double random_double(double f)
Definition: random.cpp:25
Definition: berror.cpp:12