Visual Servoing Platform  version 3.5.1 under development (2023-03-29)
vpUniRand.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Pseudo random number generator.
33  *
34  *****************************************************************************/
35 /*
36  * PCG Random Number Generation for C.
37  *
38  * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
39  *
40  * Licensed under the Apache License, Version 2.0 (the "License");
41  * you may not use this file except in compliance with the License.
42  * You may obtain a copy of the License at
43  *
44  * http://www.apache.org/licenses/LICENSE-2.0
45  *
46  * Unless required by applicable law or agreed to in writing, software
47  * distributed under the License is distributed on an "AS IS" BASIS,
48  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49  * See the License for the specific language governing permissions and
50  * limitations under the License.
51  *
52  * For additional information about the PCG random number generation scheme,
53  * including its license and other licensing options, visit
54  *
55  * http://www.pcg-random.org
56  */
57 
58 /*
59  * This code is derived from the full C implementation, which is in turn
60  * derived from the canonical C++ PCG implementation. The C++ version
61  * has many additional features and is preferable if you can use C++ in
62  * your project.
63  */
64 
65 #ifndef _vpUniRand_h_
66 #define _vpUniRand_h_
67 
68 #include <visp3/core/vpConfig.h>
69 // Visual Studio 2010 or previous is missing inttypes.h
70 #if defined(_MSC_VER) && (_MSC_VER < 1700)
71 typedef unsigned __int64 uint64_t;
72 typedef unsigned __int32 uint32_t;
73 #else
74 #include <inttypes.h>
75 #endif
76 
77 #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
78  #include <algorithm> // std::random_shuffle
79 #else
80  #include <algorithm> // std::shuffle
81  #include <random> // std::mt19937
82  #include <numeric> // std::iota
83 #endif
84 
85 #include <vector>
123 class VISP_EXPORT vpUniRand
124 {
125 private:
126  struct pcg_state_setseq_64 { // 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  pcg_state_setseq_64(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL)
132  : state(state_), inc(inc_)
133  {
134  }
135  };
136  typedef struct pcg_state_setseq_64 pcg32_random_t;
137 
138 public:
139  vpUniRand();
140  vpUniRand(uint64_t seed, uint64_t seq = 0x123465789ULL);
141 
142  double operator()();
143 
144  uint32_t next();
145  int uniform(int a, int b);
146  float uniform(float a, float b);
147  double uniform(double a, double b);
148  void setSeed(uint64_t initstate, uint64_t initseq);
149 
157  template<typename T>
158  inline static std::vector<T> shuffleVector(const std::vector<T> &inputVector)
159  {
160  std::vector<T> shuffled = inputVector;
161  #if (VISP_CXX_STANDARD <= VISP_CXX_STANDARD_11)
162  std::random_shuffle ( shuffled.begin(), shuffled.end() );
163  #else
164  std::shuffle(shuffled.begin(), shuffled.end(), std::mt19937{std::random_device{}()});
165  #endif
166  return shuffled;
167  }
168 
169 private:
170  uint32_t boundedRand(uint32_t bound);
171 
172  double m_maxInvDbl;
173  float m_maxInvFlt;
174  pcg32_random_t m_rng;
175 };
176 
177 #endif
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:124
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:158