Visual Servoing Platform  version 3.4.0
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 
100 class VISP_EXPORT vpUniRand
101 {
102 private:
103  struct pcg_state_setseq_64 { // Internals are *Private*.
104  uint64_t state; // RNG state. All values are possible.
105  uint64_t inc; // Controls which RNG sequence (stream) is
106  // selected. Must *always* be odd.
107 
108  pcg_state_setseq_64(uint64_t state_ = 0x853c49e6748fea9bULL, uint64_t inc_ = 0xda3e39cb94b95bdbULL) :
109  state(state_), inc(inc_)
110  {
111  }
112  };
113  typedef struct pcg_state_setseq_64 pcg32_random_t;
114 
115 public:
116  vpUniRand();
117  vpUniRand(uint64_t seed, uint64_t seq=0x123465789ULL);
118 
119  double operator()();
120 
121  uint32_t next();
122  int uniform(int a, int b);
123  float uniform(float a, float b);
124  double uniform(double a, double b);
125  void setSeed(uint64_t initstate, uint64_t initseq);
126 
127 private:
128  uint32_t boundedRand(uint32_t bound);
129 
130  double m_maxInvDbl;
131  float m_maxInvFlt;
132  pcg32_random_t m_rng;
133 };
134 
135 #endif
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:100