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