Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- #include <algorithm>
- #include <iostream>
- /*
- * Generates 11 sets of 6 random numbers [0..50) without copies
- */
- int main()
- {
- // seed random number generator
- std::srand(std::time(NULL));
- std::vector<int> src;
- std::vector< std::vector<int> > ret;
- for (int j = 1; j <= 49; ++j)
- src.push_back(j);
- for (size_t i = 0; i < 11; ++i) {
- // insert new vector in back
- ret.push_back(std::vector<int>());
- // work with reference to new vector, avoids later copy
- std::vector<int>& out = ret.back();
- // remaining numbers to pick from
- size_t range = src.size();
- for (size_t j = 0; j < 6; ++j) {
- // pick a value
- size_t index = std::rand() % range;
- out.push_back(src[index]);
- /* take the picked number out of range by swapping it with the last
- * value, then shrinking range
- */
- std::swap(src[index], src[range - 1]);
- range -= 1;
- }
- std::sort(out.begin(), out.end());
- }
- for (size_t i = 0; i < ret.size(); ++i) {
- for (size_t j = 0; j < ret[i].size(); ++j) {
- std::cout << ret[i][j] << " ";
- }
- std::cout << std::endl << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment