Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
vpUniRand.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 // To ensure UINT32_MAX, INT32_MX are defined on centos, ubuntu 12.04 we define __STDC_LIMIT_MACROS
66 
67 #define __STDC_LIMIT_MACROS
68 
69 #include <stdint.h>
70 #include <visp3/core/vpUniRand.h>
71 
73  : m_maxInvDbl(1.0 / static_cast<double>(UINT32_MAX)), m_maxInvFlt(1.0f / static_cast<float>(UINT32_MAX)), m_rng()
74 {
75 }
76 
85 vpUniRand::vpUniRand(uint64_t seed, uint64_t seq)
86  : m_maxInvDbl(1.0 / static_cast<double>(UINT32_MAX)), m_maxInvFlt(1.0f / static_cast<float>(UINT32_MAX)), m_rng()
87 {
88  setSeed(seed, seq);
89 }
90 
95 double vpUniRand::operator()() { return uniform(0.0, 1.0); }
96 
109 uint32_t vpUniRand::boundedRand(uint32_t bound)
110 {
111  // To avoid bias, we need to make the range of the RNG a multiple of
112  // bound, which we do by dropping output less than a threshold.
113  // A naive scheme to calculate the threshold would be to do
114  //
115  // uint32_t threshold = 0x100000000ull % bound;
116  //
117  // but 64-bit div/mod is slower than 32-bit div/mod (especially on
118  // 32-bit platforms). In essence, we do
119  //
120  // uint32_t threshold = (0x100000000ull-bound) % bound;
121  //
122  // because this version will calculate the same modulus, but the LHS
123  // value is less than 2^32.
124 
125  uint32_t threshold = -bound % bound;
126 
127  // Uniformity guarantees that this loop will terminate. In practice, it
128  // should usually terminate quickly; on average (assuming all bounds are
129  // equally likely), 82.25% of the time, we can expect it to require just
130  // one iteration. In the worst case, someone passes a bound of 2^31 + 1
131  // (i.e., 2147483649), which invalidates almost 50% of the range. In
132  // practice, bounds are typically small and only a tiny amount of the range
133  // is eliminated.
134  for (;;) {
135  uint32_t r = next();
136  if (r >= threshold) {
137  return r % bound;
138  }
139  }
140 }
141 
146 uint32_t vpUniRand::next()
147 {
148  uint64_t oldstate = m_rng.state;
149  m_rng.state = oldstate * 6364136223846793005ULL + m_rng.inc;
150  uint32_t xorshifted = static_cast<uint32_t>(((oldstate >> 18u) ^ oldstate) >> 27u);
151  uint32_t rot = oldstate >> 59u;
152  return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
153 }
154 
160 int vpUniRand::uniform(int a, int b)
161 {
162  if (a == b) {
163  return a;
164  }
165  return boundedRand(b - a) + a;
166 }
167 
173 float vpUniRand::uniform(float a, float b) { return next() * m_maxInvFlt * (b - a) + a; }
174 
180 double vpUniRand::uniform(double a, double b) { return next() * m_maxInvDbl * (b - a) + a; }
181 
203 void vpUniRand::setSeed(uint64_t initstate, uint64_t initseq)
204 {
205  m_rng.state = 0U;
206  m_rng.inc = (initseq << 1u) | 1u;
207  next();
208  m_rng.state += initstate;
209  next();
210 }
int uniform(int a, int b)
Definition: vpUniRand.cpp:160
uint32_t next()
Definition: vpUniRand.cpp:146
double operator()()
Definition: vpUniRand.cpp:95
void setSeed(uint64_t initstate, uint64_t initseq)
Definition: vpUniRand.cpp:203