I have a std::vector with roughly 1 million values stored in it. Now I want to divide the vector into N blocks with a given size and create a new std::vector by pulling randomly N blocks out of the original vector. Here is what I have so far, this is just for getting an Idea.
int main {
int breakPoint = 2;
std::vector<int> test = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector<int> newTest;
int length = test.size();
for (size_t i = 0; i < length; i++) {
int foo = random(breakPoint,length);
//std::cout << foo << std::endl;
std::vector<int> subvector(test.begin() + foo, test.begin() + foo + breakPoint);
for (size_t i = 0; i < subvector.size(); i++){
newTest.push_back(subvector[i]);
}
}
return 0;
}
int random(int N, int interval){
int rnd;
int foo = 1;
while (foo !=0) {
rnd = int(randomNumber(0, (interval+1-N)));
foo = (rnd%N);
}
return rnd;
}
randomNumber(a,b) gives an random number in the interval [a,b). This code runs and for not too large vectors I would use it this way. But since I have a large orignial vector and I will have to repeat this new vector operations many times, to gain statistics, I would rather not use this. So my question is, how to make such an operation very fast? The first problem is obviously how I select a breakpoint in random(). Thank you for helping me, cheers!
Aucun commentaire:
Enregistrer un commentaire