libtcod
Loading...
Searching...
No Matches
mersenne.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#ifndef TCOD_RANDOM_HPP_
35#define TCOD_RANDOM_HPP_
36
37#include <utility>
38
39#include "mersenne.h"
40
43// clang-format off
93
94class TCODLIB_API TCODRandom {
95 public :
110 static TCODRandom * getInstance(void);
111
124 TCODRandom(TCOD_random_algo_t algo = TCOD_RNG_CMWC, bool allocate = true);
125
161
162 // clang-format on
169 explicit TCODRandom(TCOD_Random* mersenne) : data{mersenne} {}
170
171 TCODRandom(const TCODRandom&) = delete;
172 TCODRandom& operator=(const TCODRandom&) = delete;
173 TCODRandom(TCODRandom&& rhs) : data{nullptr} { std::swap(data, rhs.data); }
174 TCODRandom& operator=(TCODRandom&& rhs) noexcept {
175 std::swap(data, rhs.data);
176 return *this;
177 }
178
179 // clang-format off
212 virtual ~TCODRandom();
213
248 inline void setDistribution (TCOD_distribution_t distribution) { TCOD_random_set_distribution(data,distribution); }
249
271 inline int getInt (int min, int max, int mean = 0) { return (mean <= 0) ? TCOD_random_get_int(data,min,max) : TCOD_random_get_int_mean(data,min,max,mean); }
272 inline int get (int min, int max, int mean = 0) { return (mean <= 0) ? TCOD_random_get_int(data,min,max) : TCOD_random_get_int_mean(data,min,max,mean); }
273
314 inline float getFloat (float min, float max, float mean = 0.0f) { return (mean <= 0) ? TCOD_random_get_float(data,min,max) : TCOD_random_get_float_mean(data,min,max,mean); }
315 inline float get (float min, float max, float mean = 0.0f) { return (mean <= 0.0f) ? TCOD_random_get_float(data,min,max) : TCOD_random_get_float_mean(data,min,max,mean); }
316
357 inline double getDouble (double min, double max, double mean = 0.0) { return (mean <= 0) ? TCOD_random_get_double(data,min,max) : TCOD_random_get_double_mean(data,min,max,mean); }
358 inline double get (double min, double max, double mean = 0.0f) { return (mean <= 0.0) ? TCOD_random_get_double(data,min,max) : TCOD_random_get_double_mean(data,min,max,mean); }
359
370 TCODRandom * save() const;
371
414 void restore(const TCODRandom *backup);
415
416 //dice
417 inline TCOD_dice_t dice (const char * s) { return TCOD_random_dice_new(s); }
418 inline int diceRoll (TCOD_dice_t dice) { return TCOD_random_dice_roll(data,dice); }
419 inline int diceRoll (const char * s) { return TCOD_random_dice_roll(data,TCOD_random_dice_new(s)); }
420
428 {
429 return data;
430 }
431 const TCOD_Random* get_data() const noexcept
432 {
433 return data;
434 }
435 protected :
436 friend class TCODLIB_API TCODNoise;
437 friend class TCODLIB_API TCODHeightMap;
438 friend class TCODLIB_API TCODNamegen;
439 friend class TCODNameGenerator; // Used for SWIG interface, does NOT need TCODLIB_API
440 TCOD_Random* data{};
441};
442
443#endif // TCOD_RANDOM_HPP_
Definition mersenne.hpp:94
virtual ~TCODRandom()
void restore(const TCODRandom *backup)
TCODRandom(TCOD_random_algo_t algo=TCOD_RNG_CMWC, bool allocate=true)
You can also create as many generators as you want with a random seed (the number of seconds since Ja...
TCODRandom * save() const
You can save the state of a generator with :
TCODRandom(TCOD_Random *mersenne)
Take ownership of a TCOD_Random* pointer.
Definition mersenne.hpp:169
TCOD_Random * get_data() noexcept
Return this objects TCOD_Random* pointer.
Definition mersenne.hpp:427
void setDistribution(TCOD_distribution_t distribution)
Definition mersenne.hpp:248
static TCODRandom * getInstance(void)
The simplest way to get random number is to use the default generator.
double getDouble(double min, double max, double mean=0.0)
Definition mersenne.hpp:357
int getInt(int min, int max, int mean=0)
Definition mersenne.hpp:271
TCODRandom(uint32_t seed, TCOD_random_algo_t algo=TCOD_RNG_CMWC)
float getFloat(float min, float max, float mean=0.0f)
Definition mersenne.hpp:314
TCOD_random_algo_t
Definition mersenne_types.h:53
union TCOD_Random TCOD_Random
Pseudorandom number generator toolkit, all attributes are private.
@ TCOD_RNG_CMWC
Complementary-Multiply-With-Carry implementation.
Definition mersenne_types.h:61
Random number generator functions.
Definition mersenne_types.h:45
Pseudorandom number generator toolkit, all attributes are private.
Definition mersenne_types.h:87