60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
// (C) Copyright Nick Thompson 2018.
|
|
// (C) Copyright Matt Borland 2021.
|
|
// Use, modification and distribution are subject to the
|
|
// Boost Software License, Version 1.0. (See accompanying file
|
|
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
#include <vector>
|
|
#include <random>
|
|
#include <type_traits>
|
|
#include <cstddef>
|
|
|
|
namespace boost { namespace math {
|
|
|
|
// To stress test, set global_seed = 0, global_size = huge.
|
|
static const std::size_t global_seed = 0;
|
|
static const std::size_t global_size = 128;
|
|
|
|
template<typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
|
std::vector<T> generate_random_vector(std::size_t size, std::size_t seed)
|
|
{
|
|
if (seed == 0)
|
|
{
|
|
std::random_device rd;
|
|
seed = rd();
|
|
}
|
|
std::vector<T> v(size);
|
|
|
|
std::mt19937 gen(seed);
|
|
|
|
std::normal_distribution<T> dis(0, 1);
|
|
for(std::size_t i = 0; i < v.size(); ++i)
|
|
{
|
|
v[i] = dis(gen);
|
|
}
|
|
return v;
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
|
|
std::vector<T> generate_random_vector(std::size_t size, std::size_t seed)
|
|
{
|
|
if (seed == 0)
|
|
{
|
|
std::random_device rd;
|
|
seed = rd();
|
|
}
|
|
std::vector<T> v(size);
|
|
|
|
std::mt19937 gen(seed);
|
|
|
|
// Rescaling by larger than 2 is UB!
|
|
std::uniform_int_distribution<T> dis(std::numeric_limits<T>::lowest()/2, (std::numeric_limits<T>::max)()/2);
|
|
for (std::size_t i = 0; i < v.size(); ++i)
|
|
{
|
|
v[i] = dis(gen);
|
|
}
|
|
return v;
|
|
}
|
|
|
|
}} // Namesapces
|