Visual Servoing Platform  version 3.6.1 under development (2024-06-12)
vpUniRand.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 _vpUniRand_h_
65 #define _vpUniRand_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 <algorithm> // std::random_shuffle
82 #endif
83 
84 #include <vector>
85 
124 class VISP_EXPORT vpUniRand
125 {
126 public:
127  vpUniRand();
128  vpUniRand(uint64_t seed, uint64_t seq = 0x123465789ULL);
129 
130  double operator()();
131 
132  uint32_t next();
133  int uniform(int a, int b);
134  float uniform(float a, float b);
135  double uniform(double a, double b);
136  void setSeed(uint64_t initstate, uint64_t initseq);
137 
145  template<typename T>
146  inline static std::vector<T> shuffleVector(const std::vector<T> &inputVector)
147  {
148  std::vector<T> shuffled = inputVector;
149 #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
150  std::random_shuffle(shuffled.begin(), shuffled.end());
151 #else
152  std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { std::random_device{}() });
153 #endif
154  return shuffled;
155  }
156 
157 private:
158  struct vpPcgStateSetseq_64_t
159  { // Internals are *Private*.
160  uint64_t state; // RNG state. All values are possible.
161  uint64_t inc; // Controls which RNG sequence (stream) is
162  // selected. Must *always* be odd.
163 
164  vpPcgStateSetseq_64_t(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL)
165  : state(state_), inc(inc_)
166  { }
167  };
168  typedef struct vpPcgStateSetseq_64_t pcg32_random_t;
169 
170 private:
171  uint32_t boundedRand(uint32_t bound);
172 
173  double m_maxInvDbl;
174  float m_maxInvFlt;
175  pcg32_random_t m_rng;
176 };
178 #endif
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:125
static std::vector< T > shuffleVector(const std::vector< T > &inputVector)
Create a new vector that is a shuffled version of the inputVector.
Definition: vpUniRand.h:146