YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
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
18using namespace yarp::sig;
19using namespace yarp::math;
20
21inline std::mt19937 *implementation(void *t)
22{
23 return static_cast<std::mt19937 *>(t);
24}
25
27{
28 impl = new std::mt19937();
29 init();
30}
31
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
50double 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
65{
66 seed=s;
67 implementation(impl)->seed(seed);
68}
float t
std::mt19937 * implementation(void *t)
RandScalar * implementation(void *t)
contains the definition of a Vector type
void init()
Initialize the random generator using current time (time(0)).
double get()
Generate a random number from a uniform distribution.