Library of Assembled Shared Sources
random.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_INL
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_INL
47
48#include "num_common.h"
49#include "random.h"
50
51#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
52# pragma warning(push)
53# pragma warning(disable: 4996) // 'deprecated-declaration': deprecation-message (or "was declared deprecated")
54#else
55# pragma GCC diagnostic push
56# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
57#endif
58
59namespace lass
60{
61namespace num
62{
63
64// --- RandomStandard ------------------------------------------------------------------------------
65
66/** draw a random number
67 */
69{
70 return rand();
71}
72
73
74
75/** draw a random number remapped to range [0, supremum)
76 * @deprecated this is A VERY BAD WAY TO DO IT!
77 */
79{
80 return rand() % supremum;
81}
82
83
84
85template <typename OutputIterator>
86OutputIterator RandomStandard::getState(OutputIterator first) const
87{
88 return first;
89}
90
91
92
93template <typename InputIterator>
94void RandomStandard::setState([[maybe_unused]] InputIterator first, [[maybe_unused]] InputIterator last)
95{
96 LASS_ASSERT(first == last);
97}
98
99
100
101// --- RandomParkMiller ----------------------------------------------------------------------------
102
103/** draw a random number remapped to range [0, supremum)
104 * @deprecated this is A VERY BAD WAY TO DO IT!
105 */
107{
108 return (*this)() % supremum;
109}
110
111
112
113template <typename OutputIterator>
114OutputIterator RandomParkMiller::getState(OutputIterator first) const
115{
116 *first = buffer_;
117 return first;
118}
119
120
121
122template <typename InputIterator>
123void RandomParkMiller::setState(InputIterator first, InputIterator LASS_UNUSED(last))
124{
125 LASS_ASSERT(first != last);
126 buffer_ = *first++;
127 LASS_ASSERT(first == last);
128}
129
130
131
132// --- RandomMT19937 -------------------------------------------------------------------------------
133
134/** construct by a range of initializing keys
135 * Requirements:
136 * @arg ForwardIterator is a forward iterator to elements of type TValue. It must really be a
137 * forward iterator and not just an input iterator, since it must be possible to iterator
138 * over the same range more than once.
139 */
140template <typename ForwardIterator>
141RandomMT19937::RandomMT19937(ForwardIterator first, ForwardIterator last)
142{
143 seed(first, last);
144}
145
146
147
148/** initialize by a range of initializing keys
149 * Requirements:
150 * @arg ForwardIterator is a forward iterator to elements of type TValue. It must really be a
151 * forward iterator and not just an input iterator, since it must be possible to iterator
152 * over the same range more than once.
153 */
154template <typename ForwardIterator>
155void RandomMT19937::seed(ForwardIterator first, ForwardIterator last)
156{
157 LASS_META_ASSERT(sizeof(TValue) * lass::bitsPerByte == 32, if_TValue_is_32_bits_then_the_wordMasks_are_not_necessary);
158
159 seed(19650218);
160
161 const size_t keySize = static_cast<size_t>(std::distance(first, last));
162 LASS_ASSERT(keySize < num::NumTraits<TValue>::max);
163
164 result_type i = 1;
165 result_type j = 0;
166 ForwardIterator key = first;
167 for (size_t k = std::max<size_t>(stateSize_, keySize); k > 0; --k)
168 {
169 state_[i] = (state_[i] ^ ((state_[i - 1] ^ (state_[i - 1] >> 30)) * 1664525)) + *key + j;
170 //state_[i] &= wordMask_;
171
172 ++i;
173 if (i >= stateSize_)
174 {
175 state_[0] = state_[stateSize_ - 1];
176 i = 1;
177 }
178
179 ++key;
180 ++j;
181 if (key == last)
182 {
183 key = first;
184 j = 0;
185 }
186 }
187 for (size_t k = stateSize_ - 1; k > 0; --k)
188 {
189 state_[i] = (state_[i] ^ ((state_[i - 1] ^ (state_[i - 1] >> 30)) * 1566083941)) - i;
190 //state_[i] &= wordMask_;
191 ++i;
192 if (i >= stateSize_)
193 {
194 state_[0] = state_[stateSize_ - 1];
195 i = 1;
196 }
197 }
198
199 state_[0] = 0x80000000;
200}
201
202
203
204/** draw a random number remapped to range [0, supremum)
205 * @deprecated this is A VERY BAD WAY TO DO IT!
206 */
208{
209 return (*this)() % supremum;
210}
211
212
213
214template <typename OutputIterator>
215OutputIterator RandomMT19937::getState(OutputIterator first) const
216{
217 *first++ = index_;
218 return std::copy(state_, state_ + stateSize_, first);
219}
220
221
222
223template <typename InputIterator>
224void RandomMT19937::setState(InputIterator first, InputIterator last)
225{
226 LASS_ASSERT(first != last);
227 index_ = *first++;
228 LASS_ASSERT(first != last);
229 [[maybe_unused]] result_type* end = std::copy(first, last, state_);
230 LASS_ASSERT(end == state_ + stateSize_);
231}
232
233
234
235}
236
237}
238
239#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
240# pragma warning(pop)
241#else
242# pragma GCC diagnostic pop
243#endif
244
245#endif
246
247// EOF
num::Tuint32 TValue
type of return value.
Definition random.h:163
void seed(result_type seed)
initializes with a seed.
Definition random.cpp:122
result_type operator()()
draw a random number
Definition random.cpp:139
num::Tuint32 result_type
type of return value.
Definition random.h:162
RandomMT19937()
default constructor.
Definition random.cpp:104
num::Tuint32 TValue
type of return value.
Definition random.h:105
result_type operator()()
draw a random number
Definition random.cpp:90
num::Tuint32 result_type
type of return value.
Definition random.h:104
int TValue
type of return value.
Definition random.h:75
result_type operator()() const
draw a random number
Definition random.inl:68
int result_type
type of return value.
Definition random.h:74
#define LASS_META_ASSERT(expression, message)
complite time static check
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53