8 #ifndef META_OCEAN_BASE_RANDOM_I_H
9 #define META_OCEAN_BASE_RANDOM_I_H
85 static unsigned int random(
const unsigned int maxValue);
93 static inline unsigned int random(
RandomGenerator& randomGenerator,
const unsigned int maxValue);
101 static int random(
const int lower,
const int upper);
109 static unsigned int random(
const unsigned int lower,
const unsigned int upper);
118 static inline int random(
RandomGenerator& randomGenerator,
const int lower,
const int upper);
127 static inline unsigned int random(
RandomGenerator& randomGenerator,
const unsigned int lower,
const unsigned int upper);
135 static void random(
const unsigned int maxValue,
unsigned int& first,
unsigned int& second);
144 static void random(
RandomGenerator& randomGenerator,
const unsigned int maxValue,
unsigned int& first,
unsigned int& second);
153 static void random(
const unsigned int maxValue,
unsigned int& first,
unsigned int& second,
unsigned int& third);
163 static void random(
RandomGenerator& randomGenerator,
const unsigned int maxValue,
unsigned int& first,
unsigned int& second,
unsigned int& third);
169 static inline bool boolean();
184 template <
typename T>
185 static T random(
const std::vector<T>& elements);
193 template <
typename T>
194 static T random(
const std::initializer_list<T>& elements);
203 template <
typename T>
204 static T random(
RandomGenerator& randomGenerator,
const std::vector<T>& elements);
213 template <
typename T>
214 static T random(
RandomGenerator& randomGenerator,
const std::initializer_list<T>& elements);
228 static constexpr
unsigned int randMax();
233 if (maxValue == (
unsigned int)(-1))
238 if (maxValue > randomGenerator.
randMax())
240 return random32(randomGenerator) % (maxValue + 1u);
244 return randomGenerator.
rand() % (maxValue + 1u);
250 ocean_assert(lower <= upper);
251 ocean_assert(int64_t(upper) - int64_t(lower) <= int64_t(std::numeric_limits<int>::max()));
253 const unsigned int range = (
unsigned int)(upper - lower);
255 if (range > randomGenerator.
randMax())
257 return lower + int(
random32(randomGenerator) % (range + 1u));
261 return lower + int(randomGenerator.
rand() % (range + 1u));
267 ocean_assert(lower <= upper);
269 const unsigned int range = upper - lower;
271 if (range == (
unsigned int)(-1))
273 ocean_assert(lower == 0u);
274 ocean_assert(upper == (
unsigned int)(-1));
279 if (range > randomGenerator.
randMax())
281 return lower +
random32(randomGenerator) % (range + 1u);
285 return lower + randomGenerator.
rand() % (range + 1u);
296 return random(randomGenerator, 1u) == 0u;
299 template <
typename T>
302 ocean_assert(!elements.empty());
304 const unsigned int index =
RandomI::random((
unsigned int)(elements.size()) - 1u);
306 return elements[index];
309 template <
typename T>
312 ocean_assert(elements.size() != 0);
314 const unsigned int index =
RandomI::random((
unsigned int)(elements.size()) - 1u);
316 return elements.begin()[index];
319 template <
typename T>
322 ocean_assert(!elements.empty());
324 const unsigned int index =
RandomI::random(randomGenerator, (
unsigned int)(elements.size()) - 1u);
326 return elements[index];
329 template <
typename T>
332 ocean_assert(elements.size() != 0);
334 const unsigned int index =
RandomI::random(randomGenerator, (
unsigned int)(elements.size()) - 1u);
336 return elements.begin()[index];
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
unsigned int rand()
Returns the next random number.
Definition: RandomGenerator.h:161
static constexpr unsigned int randMax()
Returns the maximal random value of this generator.
Definition: RandomGenerator.h:183
This class provides base random functions and several random functions for integer data types.
Definition: RandomI.h:29
static int random(const int lower, const int upper)
Returns one random integer value within a specific range.
static void random(const unsigned int maxValue, unsigned int &first, unsigned int &second)
Returns two different random integer values with specified maximum value.
static constexpr unsigned int randMax()
Returns the maximal random value of the default random number generator.
Definition: RandomI.h:339
static unsigned int timeBasedSeed()
Returns a seed value based on the current time.
static uint64_t random64(RandomGenerator &randomGenerator)
Returns one random integer number with range [0x00000000 00000000, 0xFFFFFFFF FFFFFFFF] using an expl...
static uint32_t random32(RandomGenerator &randomGenerator)
Returns one random integer number with range [0x00000000, 0xFFFFFFFF] using an explicit random genera...
static uint32_t random32()
Returns one random integer number with range [0x00000000, 0xFFFFFFFF].
static void initialize()
Initializes the standard random generator for this thread with a time-based seed value.
static void random(RandomGenerator &randomGenerator, const unsigned int maxValue, unsigned int &first, unsigned int &second, unsigned int &third)
Returns three different random integer values with specified maximum value using an explicit random g...
static uint64_t random64()
Returns one random integer number with range [0x00000000 00000000, 0xFFFFFFFF FFFFFFFF].
static void initialize(const unsigned int value)
Initializes the standard random generator for this thread with a user-defined seed value.
static unsigned int random(const unsigned int lower, const unsigned int upper)
Returns one random integer value within a specific range.
static unsigned int random(const unsigned int maxValue)
Returns one random integer value with specified maximum value.
static void random(RandomGenerator &randomGenerator, const unsigned int maxValue, unsigned int &first, unsigned int &second)
Returns two different random integer values with specified maximum value using an explicit random gen...
static void random(const unsigned int maxValue, unsigned int &first, unsigned int &second, unsigned int &third)
Returns three different random integer values with specified maximum value.
static bool boolean()
Returns a random bool value.
Definition: RandomI.h:289
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15