Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Randomizer Class Reference

#include <Randomizer.hxx>

+ Inheritance diagram for Randomizer:
+ Collaboration diagram for Randomizer:

Public Member Functions

 Randomizer ()=default
 
template<typename Iterator >
void shuffle (Iterator begin, Iterator end)
 
template<typename Iterator >
Iterator choose (Iterator begin, Iterator end)
 Pick random item from container. More...
 

Private Types

template<typename IntType >
using UniformIntDist = std::uniform_int_distribution< IntType >
 

Static Private Attributes

static thread_local std::mt19937 generator {std::random_device{}()}
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< Randomizer >
static Randomizerinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< Randomizer >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

Definition at line 9 of file Randomizer.hxx.

Member Typedef Documentation

◆ UniformIntDist

template<typename IntType >
using Randomizer::UniformIntDist = std::uniform_int_distribution<IntType>
private

Definition at line 13 of file Randomizer.hxx.

Constructor & Destructor Documentation

◆ Randomizer()

Randomizer::Randomizer ( )
default

Member Function Documentation

◆ choose()

template<typename Iterator >
Iterator Randomizer::choose ( Iterator  begin,
Iterator  end 
)
inline

Pick random item from container.

@threadsafe

Parameters
beginthe starting iterator
endthe pointer following the last value
Returns
a random iterator between [begin, end)

Definition at line 29 of file Randomizer.hxx.

30  {
31  using DiffType = typename std::iterator_traits<Iterator>::difference_type;
32  DiffType upper{std::distance(begin, end) - 1};
33  UniformIntDist<DiffType> distn{DiffType{0}, upper};
34  std::advance(begin, distn(generator));
35  return begin;
36  }
+ Here is the caller graph for this function:

◆ shuffle()

template<typename Iterator >
void Randomizer::shuffle ( Iterator  begin,
Iterator  end 
)
inline

Definition at line 18 of file Randomizer.hxx.

18 { std::shuffle(begin, end, generator); }

Member Data Documentation

◆ generator

thread_local std::mt19937 Randomizer::generator {std::random_device{}()}
staticprivate

Definition at line 11 of file Randomizer.hxx.


The documentation for this class was generated from the following files:
Randomizer::generator
static thread_local std::mt19937 generator
Definition: Randomizer.hxx:11