cxtream  0.5.1
C++17 data pipeline with Python bindings.
random_fill.hpp
1 /****************************************************************************
2  * cxtream library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Author(s) Filip Matzner
5  *
6  * This file is distributed under the MIT License.
7  * See the accompanying file LICENSE.txt for the complete license agreement.
8  ****************************************************************************/
9 
10 #ifndef CXTREAM_CORE_STREAM_RANDOM_FILL_HPP
11 #define CXTREAM_CORE_STREAM_RANDOM_FILL_HPP
12 
13 #include <cxtream/core/stream/generate.hpp>
14 
15 #include <functional>
16 #include <random>
17 
18 namespace cxtream::stream {
19 
51 template<typename FromColumn, typename ToColumn, typename Prng = std::mt19937,
52  typename Dist = std::uniform_real_distribution<double>,
53  int Dim = utility::ndims<typename ToColumn::batch_type>::value
54  - utility::ndims<std::result_of_t<Dist(Prng&)>>::value>
55 constexpr auto random_fill(from_t<FromColumn> size_from,
56  to_t<ToColumn> fill_to,
57  long rnddims = std::numeric_limits<long>::max(),
58  Dist dist = Dist{0, 1},
60  dim_t<Dim> d = dim_t<Dim>{})
61 {
62  // distribution is always copied to avoid race conditions
63  auto fun = [dist, &prng]() { return std::invoke(Dist{dist}, prng); };
64  return stream::generate(size_from, fill_to, std::move(fun), rnddims, d);
65 }
66 
67 } // namespace cxtream::stream
68 #endif
static thread_local std::mt19937 random_generator
Thread local pseudo-random number generator seeded by std::random_device.
Definition: random.hpp:20
constexpr void generate(Rng &&rng, Gen &&gen, long gendims=std::numeric_limits< long >::max())
Fill a multidimensional range with values generated by a nullary function.
Definition: vector.hpp:605
constexpr void random_fill(Rng &&rng, Dist &&dist=Dist{0, 1}, Prng &&prng=utility::random_generator, long gendims=std::numeric_limits< long >::max())
Fill a multidimensional range with random values.
Definition: vector.hpp:659