Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
vpUniRand.h
1
/*
2
* ViSP, open source Visual Servoing Platform software.
3
* Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4
*
5
* This software is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
* See the file LICENSE.txt at the root directory of this source
10
* distribution for additional information about the GNU GPL.
11
*
12
* For using ViSP with software that can not be combined with the GNU
13
* GPL, please contact Inria about acquiring a ViSP Professional
14
* Edition License.
15
*
16
* See https://visp.inria.fr for more information.
17
*
18
* This software was developed at:
19
* Inria Rennes - Bretagne Atlantique
20
* Campus Universitaire de Beaulieu
21
* 35042 Rennes Cedex
22
* France
23
*
24
* If you have questions regarding the use of this file, please contact
25
* Inria at visp@inria.fr
26
*
27
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29
*
30
* Description:
31
* Pseudo random number generator.
32
*/
33
34
/*
35
* PCG Random Number Generation for C.
36
*
37
* Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
38
*
39
* Licensed under the Apache License, Version 2.0 (the "License");
40
* you may not use this file except in compliance with the License.
41
* You may obtain a copy of the License at
42
*
43
* http://www.apache.org/licenses/LICENSE-2.0
44
*
45
* Unless required by applicable law or agreed to in writing, software
46
* distributed under the License is distributed on an "AS IS" BASIS,
47
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48
* See the License for the specific language governing permissions and
49
* limitations under the License.
50
*
51
* For additional information about the PCG random number generation scheme,
52
* including its license and other licensing options, visit
53
*
54
* http://www.pcg-random.org
55
*/
56
57
/*
58
* This code is derived from the full C implementation, which is in turn
59
* derived from the canonical C++ PCG implementation. The C++ version
60
* has many additional features and is preferable if you can use C++ in
61
* your project.
62
*/
63
64
#ifndef VP_UNIRAND_H
65
#define VP_UNIRAND_H
66
67
#include <visp3/core/vpConfig.h>
68
// Visual Studio 2010 or previous is missing inttypes.h
69
#if defined(_MSC_VER) && (_MSC_VER < 1700)
70
typedef
unsigned
__int64 uint64_t;
71
typedef
unsigned
__int32 uint32_t;
72
#else
73
#include <inttypes.h>
74
#endif
75
76
#if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_11)
77
#include <algorithm>
// std::shuffle
78
#include <random>
// std::mt19937
79
#include <numeric>
// std::iota
80
#else
81
#include <ctime>
// std::time
82
#include <cstdlib>
// std::rand, std::srand
83
#include <algorithm>
// std::random_shuffle
84
#endif
85
86
#include <vector>
87
88
BEGIN_VISP_NAMESPACE
126
class
VISP_EXPORT
vpUniRand
127
{
128
public
:
129
vpUniRand
();
130
vpUniRand
(uint64_t seed, uint64_t seq = 0x123465789ULL);
131
132
double
operator()
();
133
134
uint32_t
next
();
135
int
uniform
(
int
a,
int
b);
136
float
uniform
(
float
a,
float
b);
137
double
uniform
(
double
a,
double
b);
138
void
setSeed
(uint64_t initstate, uint64_t initseq);
139
140
141
std::vector<size_t>
sampleWithoutReplacement
(
size_t
count,
size_t
vectorSize);
142
151
template
<
typename
T>
152
inline
static
std::vector<T>
shuffleVector
(
const
std::vector<T> &inputVector,
const
int32_t &seed = -1)
153
{
154
std::vector<T> shuffled = inputVector;
155
#if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
156
if
(seed > 0) {
157
std::srand(seed);
158
}
159
else
{
160
std::srand(std::time(0));
161
}
162
std::random_shuffle(shuffled.begin(), shuffled.end());
163
#else
164
if
(seed < 0) {
165
std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { std::random_device{}() });
166
}
167
else
{
168
std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { static_cast<uint32_t>(seed) });
169
}
170
#endif
171
return
shuffled;
172
}
173
174
private
:
175
struct
vpPcgStateSetSeq64t
176
{
// Internals are *Private*.
177
uint64_t state;
// RNG state. All values are possible.
178
uint64_t inc;
// Controls which RNG sequence (stream) is
179
// selected. Must *always* be odd.
180
181
vpPcgStateSetSeq64t(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL)
182
: state(state_), inc(inc_)
183
{ }
184
};
185
typedef
struct
vpPcgStateSetSeq64t pcg32_random_t;
186
187
private
:
188
uint32_t boundedRand(uint32_t bound);
189
190
double
m_maxInvDbl;
191
float
m_maxInvFlt;
192
pcg32_random_t m_rng;
193
};
194
END_VISP_NAMESPACE
195
#endif
vpUniRand::vpUniRand
vpUniRand()
Definition
vpUniRand.cpp:72
vpUniRand::uniform
int uniform(int a, int b)
Definition
vpUniRand.cpp:161
vpUniRand::next
uint32_t next()
Definition
vpUniRand.cpp:145
vpUniRand::sampleWithoutReplacement
std::vector< size_t > sampleWithoutReplacement(size_t count, size_t vectorSize)
Definition
vpUniRand.cpp:214
vpUniRand::shuffleVector
static std::vector< T > shuffleVector(const std::vector< T > &inputVector, const int32_t &seed=-1)
Create a new vector that is a shuffled version of the inputVector.
Definition
vpUniRand.h:152
vpUniRand::operator()
double operator()()
Definition
vpUniRand.cpp:94
vpUniRand::setSeed
void setSeed(uint64_t initstate, uint64_t initseq)
Definition
vpUniRand.cpp:204
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
core
include
visp3
core
vpUniRand.h
Generated by
1.17.0