HiPipe  0.7.0
C++17 data pipeline with Python bindings.
random_fill.hpp
1 /****************************************************************************
2  * hipipe library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Copyright (c) 2018, Iterait a.s.
5  * Author(s) Filip Matzner
6  *
7  * This file is distributed under the MIT License.
8  * See the accompanying file LICENSE.txt for the complete license agreement.
9  ****************************************************************************/
10 
11 #pragma once
12 
13 #include <hipipe/core/stream/generate.hpp>
14 
15 namespace hipipe::stream {
16 
48 template<typename FromColumn, typename ToColumn, typename Prng = std::mt19937,
49  typename Dist = std::uniform_real_distribution<double>,
50  int Dim = utility::ndims<typename ToColumn::data_type>::value
51  - utility::ndims<std::result_of_t<Dist(Prng&)>>::value>
52 auto random_fill(from_t<FromColumn> size_from,
53  to_t<ToColumn> fill_to,
54  long rnddims = std::numeric_limits<long>::max(),
55  Dist dist = Dist{0, 1},
57  dim_t<Dim> d = dim_t<Dim>{})
58 {
59  // a bit of function type erasure to speed up compilation
60  using GenT = std::function<
61  utility::ndim_type_t<typename ToColumn::data_type, Dim>()>;
62  // distribution is always copied to avoid race conditions
63  GenT 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 hipipe::stream
hipipe::utility::random_generator
static thread_local std::mt19937 random_generator
Thread local pseudo-random number generator seeded by std::random_device.
Definition: random.hpp:20
random_fill
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: ndim.hpp:682
generate
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: ndim.hpp:628