From 44818615ad603287c06ce790c69308b8887622c6 Mon Sep 17 00:00:00 2001 From: Rene Milk <rene.milk@uni-muenster.de> Date: Wed, 15 Aug 2012 15:33:16 +0200 Subject: [PATCH] [common] introduce new random header with convience wrapper around std::random dists --- dune/stuff/common/random.hh | 122 ++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 dune/stuff/common/random.hh diff --git a/dune/stuff/common/random.hh b/dune/stuff/common/random.hh new file mode 100644 index 000000000..ddc42ad3c --- /dev/null +++ b/dune/stuff/common/random.hh @@ -0,0 +1,122 @@ +#ifndef DUNE_STUFF_RANDOM_HH +#define DUNE_STUFF_RANDOM_HH + +#include <random> +#include <boost/assign/list_of.hpp> + +namespace Dune { +namespace Stuff { +namespace Common { +namespace Math { + +template <typename T, bool = std::is_integral<T>::value> +struct UniformDistributionSelector +{ +}; + +template <typename T> +struct UniformDistributionSelector<T, true> +{ + typedef std::uniform_int_distribution<T> type; +}; +template <typename T> +struct UniformDistributionSelector<T, false> +{ + typedef std::uniform_real_distribution<T> type; +}; + +template <class T, class DistributionImp, class EngineImp> +struct RNG +{ + typedef DistributionImp DistributionType; + typedef EngineImp EngineType; + EngineType generator; + DistributionType distribution; + RNG(EngineType g, DistributionType d) + : generator(g) + , distribution(d) + { + } + + T operator()() + { + return distribution(generator); + } +}; + +template <class T> +class DefaultRNG : public RNG<T, typename UniformDistributionSelector<T>::type, std::default_random_engine> +{ + typedef RNG<T, typename UniformDistributionSelector<T>::type, std::default_random_engine> BaseType; + +public: + DefaultRNG(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) + : BaseType(std::default_random_engine(std::random_device()()), + typename UniformDistributionSelector<T>::type(min, max)) + { + } +}; + +namespace { +const std::string alphanums("abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "1234567890"); +const std::string other_printables("!@#$%^&*()" + "`~-_=+[{]{\\|;:'\",<.>/? "); +} + +class RandomStrings : public RNG<std::string, std::uniform_int_distribution<int>, std::mt19937> +{ + typedef RNG<std::string, std::uniform_int_distribution<int>, std::mt19937> BaseType; + const int length; + +public: + RandomStrings(int l) + : BaseType(std::mt19937(std::random_device()()), std::uniform_int_distribution<int>(0, alphanums.size() - 1)) + , length(l) + { + } + + std::string operator()() + { + std::string ret(length, '\0'); + std::generate(std::begin(ret), std::end(ret), [=]() { return alphanums[distribution(generator)]; }); + return ret; + } +}; + + +} // namespace Math +} // namespace Common +} // namespace Stuff +} // namespace Dune + +#endif // DUNE_STUFF_RANDOM_HH + +/** Copyright (c) 2012, Rene Milk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the FreeBSD Project. + **/ -- GitLab