Answer the question
In order to leave comments, you need to log in
C++ how to fill a vector with randomness?
Can't fill Vector with random numbers. With static like this:
void rand_arr()
{
const int size(100);
int iArr[size] = { 0 };
int i(0), j(0);
cout << "\n Массив заполненный случайными числами :\n";
top(); //Числа сверху массива
for (i = 0; i < size; i++)
{
if ((i == 0) or (i % 10 == 0)); // Выводить на экран строки по 10 значений
{
cout << endl << " " << j << setw(3) << '|'; // Для чисел слева
j++;
}
iArr[i] = round(rand() % 21); // Рандом от 0 до 20
cout << setw(5) << iArr[i];
}
}
Answer the question
In order to leave comments, you need to log in
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;
vector<int> randomVector(size_t size)
{
vector<int> v(size);
random_device r;
generate(v.begin(), v.end(), [&]{return r();});
return v;
}
int main()
{
vector<int> v(randomVector(100));
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question