Add array randomisation helper routine
This commit is contained in:
parent
2783bcaa23
commit
db6c603517
@ -76,6 +76,7 @@ UTIL_FILES = \
|
|||||||
raii.hpp \
|
raii.hpp \
|
||||||
random.cpp \
|
random.cpp \
|
||||||
random.hpp \
|
random.hpp \
|
||||||
|
random.ipp \
|
||||||
range.cpp \
|
range.cpp \
|
||||||
range.hpp \
|
range.hpp \
|
||||||
region.cpp \
|
region.cpp \
|
||||||
|
16
random.cpp
16
random.cpp
@ -22,13 +22,27 @@
|
|||||||
|
|
||||||
#include "range.hpp"
|
#include "range.hpp"
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
using namespace util;
|
using namespace util;
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T
|
T
|
||||||
|
random (void) {
|
||||||
|
static_assert (std::is_integral<T>::value, "random should only operate on integral types");
|
||||||
|
return range<T>::UNLIMITED.random ();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
double
|
||||||
random (void)
|
random (void)
|
||||||
{ return range<T>::UNLIMITED.random (); }
|
{ return range<double>::UNIT.random (); }
|
||||||
|
|
||||||
|
template <>
|
||||||
|
float
|
||||||
|
random (void)
|
||||||
|
{ return range<float>::UNIT.random (); }
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T&
|
T&
|
||||||
|
@ -26,6 +26,9 @@ namespace util {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
T& randomise (T &);
|
T& randomise (T &);
|
||||||
|
|
||||||
|
template <typename T, size_t N>
|
||||||
|
T* randomise (T(&)[N]);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T random (void);
|
T random (void);
|
||||||
|
|
||||||
@ -49,4 +52,6 @@ namespace util {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "random.ipp"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
32
random.ipp
Normal file
32
random.ipp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of libgim.
|
||||||
|
*
|
||||||
|
* libgim is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_RANDOM_IPP
|
||||||
|
#define __UTIL_RANDOM_IPP
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
template <typename T, size_t N>
|
||||||
|
T* randomise (T (&array)[N]) {
|
||||||
|
for (auto &i: array)
|
||||||
|
i = random<T> ();
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user