Vitaly
0
Q:

choose random elements from vector without repetition and adding to another vector c++

#include <random>

vector <int> _myvec1{1,2,3,4,5,6};
vector <int> _myvec2{-5,-3,10,-8};

int n = 3;  //3 elements will be added
random_device rd;
mt19937 g(rd());

shuffle(_myvec2.begin(), _myvec2.end(), g);
for (int i=0; i<n; i++){
  _myvec1.push_back(_myvec2[i]);
}
0

New to Communities?

Join the community