Visual Servoing Platform  version 3.6.1 under development (2024-10-15)
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 
148  template<typename T>
149  inline static std::vector<T> shuffleVector(const std::vector<T> &inputVector, const int32_t &seed = -1)
150  {
151  std::vector<T> shuffled = inputVector;
152 #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
153  if (seed > 0) {
154  std::srand(seed);
155  }
156  else {
157  std::srand(std::time(0));
158  }
159  std::random_shuffle(shuffled.begin(), shuffled.end());
160 #else
161  if (seed < 0) {
162  std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { std::random_device{}() });
163  }
164  else {
165  std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { static_cast<uint32_t>(seed) });
166  }
167 #endif
168  return shuffled;
169  }
170 
171 private:
172  struct vpPcgStateSetSeq64t
173  { // Internals are *Private*.
174  uint64_t state; // RNG state. All values are possible.
175  uint64_t inc; // Controls which RNG sequence (stream) is
176  // selected. Must *always* be odd.
177 
178  vpPcgStateSetSeq64t(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL)
179  : state(state_), inc(inc_)
180  { }
181  };
182  typedef struct vpPcgStateSetSeq64t pcg32_random_t;
183 
184 private:
185  uint32_t boundedRand(uint32_t bound);
186 
187  double m_maxInvDbl;
188  float m_maxInvFlt;
189  pcg32_random_t m_rng;
190 };
191 END_VISP_NAMESPACE
192 #endif
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:127
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:149