YARP
Yet Another Robot Platform
RandScalar.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 // Sept. 2010 Uses gsl routines for random generation.
8 
9 #include <yarp/math/RandScalar.h>
10 #include <yarp/sig/Vector.h>
11 #include <ctime>
12 #include <cstdio>
13 #include <cmath>
14 
15 // implementation of Marsenne Twister from C++11's library
16 #include <random>
17 
18 using namespace yarp::sig;
19 using namespace yarp::math;
20 
21 inline std::mt19937 *implementation(void *t)
22 {
23  return static_cast<std::mt19937 *>(t);
24 }
25 
26 RandScalar::RandScalar()
27 {
28  impl = new std::mt19937();
29  init();
30 }
31 
32 RandScalar::RandScalar(int s) :
33  seed(s)
34 {
35  impl = new std::mt19937;
36  implementation(impl)->seed(seed);
37 }
38 
40 {
41  delete implementation(impl);
42 }
43 
45 {
46  std::uniform_real_distribution<double> dist(0.0, 1.0);
47  return dist(*(implementation(impl)));
48 }
49 
50 double RandScalar::get(double min, double max)
51 {
52  std::uniform_real_distribution<double> dist(min, max);
53  return dist(*(implementation(impl)));
54 }
55 
56 // initialize with a call to "time"
58 {
59  // initialize with time
60  int t=(int)time(nullptr);
62 }
63 
64 void RandScalar::init(int s)
65 {
66  seed=s;
67  implementation(impl)->seed(seed);
68 }
float t
std::mt19937 * implementation(void *t)
Definition: RandScalar.cpp:21
contains the definition of a Vector type
void init()
Initialize the random generator using current time (time(0)).
Definition: RandScalar.cpp:57
double get()
Generate a random number from a uniform distribution.
Definition: RandScalar.cpp:44
Signal processing.
Definition: Image.h:22