vendredi 10 novembre 2017

Non-repeating random numbers in vector C++

I'm trying to store random numbers in vector, but I want each number to be unique. Can I do that with for loop without using unique() or random_shuffle() ?

#include <iostream>
#include <vector>
#include <ctime>

using namespace std;

int main()
{

    srand(time(NULL));

    vector<int> v;

    for (unsigned int i = 0; i < 30; i++) {

          v.push_back(rand() % 30);
    }

    for (unsigned int j = 0; j < 30; j++) {

        cout << v[j] << endl;
    }

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire