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