Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
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>
122 class VISP_EXPORT vpUniRand
123 {
124 private:
125  struct vpPcgStateSetseq_64_t
126  { // Internals are *Private*.
127  uint64_t state; // RNG state. All values are possible.
128  uint64_t inc; // Controls which RNG sequence (stream) is
129  // selected. Must *always* be odd.
130 
131  vpPcgStateSetseq_64_t(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL)
132  : state(state_), inc(inc_)
133  { }
134  };
135  typedef struct vpPcgStateSetseq_64_t pcg32_random_t;
136 
137 public:
138  vpUniRand();
139  vpUniRand(uint64_t seed, uint64_t seq = 0x123465789ULL);
140 
141  double operator()();
142 
143  uint32_t next();
144  int uniform(int a, int b);
145  float uniform(float a, float b);
146  double uniform(double a, double b);
147  void setSeed(uint64_t initstate, uint64_t initseq);
148 
156  template<typename T>
157  inline static std::vector<T> shuffleVector(const std::vector<T> &inputVector)
158  {
159  std::vector<T> shuffled = inputVector;
160 #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
161  std::random_shuffle(shuffled.begin(), shuffled.end());
162 #else
163  std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937 { std::random_device{}() });
164 #endif
165  return shuffled;
166  }
167 
168 private:
169  uint32_t boundedRand(uint32_t bound);
170 
171  double m_maxInvDbl;
172  float m_maxInvFlt;
173  pcg32_random_t m_rng;
174 };
175 
176 #endif
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:123
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:157