YARP
Yet Another Robot Platform
Rand.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 #include <yarp/math/Rand.h>
8 
9 #include <ctime>
10 #include <cstdio>
11 #include <cmath>
12 #include <mutex>
13 
14 using namespace yarp::os;
15 using namespace yarp::sig;
16 using namespace yarp::math;
17 using namespace yarp::math::impl;
18 
19 /*
20 * This class was used in the past to wrap random generation
21 * routines that were not thread safe. Nowadays it could be no
22 * longer required because gsl routines are already declared to be
23 * thread safe.
24 */
26 {
27  std::mutex mutex;
28 public:
30  {
31 
32  }
33 
34  void init()
35  {
36  mutex.lock();
37  RandScalar::init();
38  mutex.unlock();
39  }
40 
41  void init(int s)
42  {
43  mutex.lock();
44  RandScalar::init(s);
45  mutex.unlock();
46  }
47 
48  double get(double min=0.0, double max=1.0)
49  {
50  double ret;
51  mutex.lock();
52  ret=RandScalar::get(min, max);
53  mutex.unlock();
54  return ret;
55  }
56 
58 
59 double Rand::scalar()
60 {
61  return theRandScalar.get();
62 }
63 
64 double Rand::scalar(double min, double max)
65 {
66  return theRandScalar.get(min, max);
67 }
68 
69 void Rand::init()
70 {
72 }
73 
74 void Rand::init(int seed)
75 {
76  theRandScalar.init(seed);
77 }
78 
79 Vector Rand::vector(int s)
80 {
81  yarp::sig::Vector ret((size_t) s);
82  for(int k=0;k<s;k++)
83  {
84  ret[k]=theRandScalar.get();
85  }
86 
87  return ret;
88 }
89 
90 Vector Rand::vector(const Vector &min, const Vector &max)
91 {
92  size_t s = min.size();
94  for(size_t k=0;k<s;k++)
95  {
96  ret[k]=theRandScalar.get(min[k], max[k]);
97  }
98 
99  return ret;
100 }
101 
102 Matrix Rand::matrix(int rows, int cols)
103 {
104  yarp::sig::Matrix ret(rows,cols);
105  for (int r = 0; r < rows; r++) {
106  for(int c=0;c<cols;c++)
107  {
108  ret[r][c]=theRandScalar.get();
109  }
110  }
111 
112  return ret;
113 }
bool ret
ThreadSafeRandScalar theRandScalar
double get(double min=0.0, double max=1.0)
Definition: Rand.cpp:48
void init(int s)
Definition: Rand.cpp:41
A random number generator, uniform in the range 0-1.
Definition: RandScalar.h:29
A class for a Matrix.
Definition: Matrix.h:43
size_t size() const
Definition: Vector.h:323
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22