[Solved] Can I create a loop which fills 100 arrays with six random numbers [closed]


If you want to have a random number of arrays you have to store them in a resizable container like a vector, containing the arrays with the six elements. Resize this vector with this random number and then iterate through it and add random numbers

#include <array>
#include <random>

namespace
{
  constexpr auto NumberOfArrayElements = 6;
  constexpr auto MaximumNumberOfArrays = 100;

  template<class VectorT>
  void resize_randomly(VectorT& vector)
  {
    std::random_device random_device;
    std::mt19937 engine(random_device());
    std::uniform_int_distribution<VectorT::size_type> 
      distribution(0, MaximumNumberOfArrays > vector.max_size() ? vector.max_size() : MaximumNumberOfArrays);

    vector.resize(distribution(engine));
  }

  void fill_randomly(std::vector<std::array<int, NumberOfArrayElements>>& vector_of_arrays)
  {
    std::random_device random_device;
    std::mt19937 engine(random_device());
    std::uniform_int_distribution<int> distribution(std::numeric_limits<int>::lowest(), std::numeric_limits<int>::max());

    for (auto& array : vector_of_arrays)
      std::generate(array.begin(), array.end(), [&distribution, &engine]() { return distribution(engine); });
  }
}

auto main() -> int 
{
  std::vector<std::array<int, NumberOfArrayElements>> vector_of_arrays;
  resize_randomly(vector_of_arrays);
  fill_randomly(vector_of_arrays);
}

Use the C++11 random feature to generate random numbers.

solved Can I create a loop which fills 100 arrays with six random numbers [closed]