Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
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  * PCG Random Number Generation for C.
35  *
36  * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
37  *
38  * Licensed under the Apache License, Version 2.0 (the "License");
39  * you may not use this file except in compliance with the License.
40  * You may obtain a copy of the License at
41  *
42  * http://www.apache.org/licenses/LICENSE-2.0
43  *
44  * Unless required by applicable law or agreed to in writing, software
45  * distributed under the License is distributed on an "AS IS" BASIS,
46  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47  * See the License for the specific language governing permissions and
48  * limitations under the License.
49  *
50  * For additional information about the PCG random number generation scheme,
51  * including its license and other licensing options, visit
52  *
53  * http://www.pcg-random.org
54  */
55 
56 /*
57  * This code is derived from the full C implementation, which is in turn
58  * derived from the canonical C++ PCG implementation. The C++ version
59  * has many additional features and is preferable if you can use C++ in
60  * your project.
61  */
62 
63 #ifndef _vpUniRand_h_
64 #define _vpUniRand_h_
65 
66 #include <visp3/core/vpConfig.h>
67 // Visual Studio 2010 or previous is missing inttypes.h
68 #if defined(_MSC_VER) && (_MSC_VER < 1700)
69 typedef unsigned __int64 uint64_t;
70 typedef unsigned __int32 uint32_t;
71 #else
72 #include <inttypes.h>
73 #endif
74 
75 #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
76 #include <algorithm> // std::random_shuffle
77 #else
78 #include <algorithm> // std::shuffle
79 #include <random> // std::mt19937
80 #include <numeric> // std::iota
81 #endif
82 
83 #include <vector>
121 class VISP_EXPORT vpUniRand
122 {
123 private:
124  struct vpPcgStateSetseq_64_t
125  { // Internals are *Private*.
126  uint64_t state; // RNG state. All values are possible.
127  uint64_t inc; // Controls which RNG sequence (stream) is
128  // selected. Must *always* be odd.
129 
130  vpPcgStateSetseq_64_t(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL)
131  : state(state_), inc(inc_)
132  { }
133  };
134  typedef struct vpPcgStateSetseq_64_t pcg32_random_t;
135 
136 public:
137  vpUniRand();
138  vpUniRand(uint64_t seed, uint64_t seq = 0x123465789ULL);
139 
140  double operator()();
141 
142  uint32_t next();
143  int uniform(int a, int b);
144  float uniform(float a, float b);
145  double uniform(double a, double b);
146  void setSeed(uint64_t initstate, uint64_t initseq);
147 
155  template<typename T>
156  inline static std::vector<T> shuffleVector(const std::vector<T> &inputVector)
157  {
158  std::vector<T> shuffled = inputVector;
159 #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
160  std::random_shuffle(shuffled.begin(), shuffled.end());
161 #else
162  std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { std::random_device{}() });
163 #endif
164  return shuffled;
165  }
166 
167 private:
168  uint32_t boundedRand(uint32_t bound);
169 
170  double m_maxInvDbl;
171  float m_maxInvFlt;
172  pcg32_random_t m_rng;
173 };
174 
175 #endif
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:122
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:156