YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
14using namespace yarp::os;
15using namespace yarp::sig;
16using namespace yarp::math;
17using 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;
28public:
33
34 void init()
35 {
36 mutex.lock();
38 mutex.unlock();
39 }
40
41 void init(int s)
42 {
43 mutex.lock();
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
60{
61 return theRandScalar.get();
62}
63
64double Rand::scalar(double min, double max)
65{
66 return theRandScalar.get(min, max);
67}
68
70{
72}
73
74void Rand::init(int seed)
75{
76 theRandScalar.init(seed);
77}
78
80{
81 yarp::sig::Vector ret((size_t) s);
82 for(int k=0;k<s;k++)
83 {
85 }
86
87 return ret;
88}
89
90Vector 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
102Matrix 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:27
void init()
Initialize the random generator using current time (time(0)).
double get()
Generate a random number from a uniform distribution.
static yarp::sig::Vector vector(int s)
Get a vector of random numbers from a uniform distribution, values are in the range [0,...
Definition Rand.cpp:79
static void init()
Initialize the random number generator, with current time (time(0)).
Definition Rand.cpp:69
static double scalar()
Get a random number from a uniform distribution in the range [0,1].
Definition Rand.cpp:59
static yarp::sig::Matrix matrix(int r, int c)
Get a vector of random numbers from a uniform distribution, values are in the range [0,...
Definition Rand.cpp:102
A mini-server for performing network communication in the background.
A class for a Matrix.
Definition Matrix.h:39
size_t size() const
Definition Vector.h:322
An interface to the operating system, including Port based communication.