diff --git a/Makefile.am b/Makefile.am index 7ffe6409..2881cb29 100644 --- a/Makefile.am +++ b/Makefile.am @@ -76,6 +76,7 @@ UTIL_FILES = \ raii.hpp \ random.cpp \ random.hpp \ + random.ipp \ range.cpp \ range.hpp \ region.cpp \ diff --git a/random.cpp b/random.cpp index 49efb1fd..f07a0733 100644 --- a/random.cpp +++ b/random.cpp @@ -22,13 +22,27 @@ #include "range.hpp" +#include + using namespace util; namespace util { template T + random (void) { + static_assert (std::is_integral::value, "random should only operate on integral types"); + return range::UNLIMITED.random (); + } + + template <> + double random (void) - { return range::UNLIMITED.random (); } + { return range::UNIT.random (); } + + template <> + float + random (void) + { return range::UNIT.random (); } template T& diff --git a/random.hpp b/random.hpp index 43b325e1..9ade838f 100644 --- a/random.hpp +++ b/random.hpp @@ -26,6 +26,9 @@ namespace util { template T& randomise (T &); + template + T* randomise (T(&)[N]); + template T random (void); @@ -49,4 +52,6 @@ namespace util { } } +#include "random.ipp" + #endif diff --git a/random.ipp b/random.ipp new file mode 100644 index 00000000..d72d9435 --- /dev/null +++ b/random.ipp @@ -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 . + * + * Copyright 2010 Danny Robson + */ + +#ifndef __UTIL_RANDOM_IPP +#define __UTIL_RANDOM_IPP + +namespace util { + template + T* randomise (T (&array)[N]) { + for (auto &i: array) + i = random (); + return array; + } +} + +#endif