vendredi 8 février 2019

Generating Unique Random Numbers in C++

C++ beginner here. And I have this code below. It's more than that but this is the part where I need help. I need to generate random numbers( 0 - 9 )and push it back into a vector. And I want those random numbers to be unique. How do I do that? Also, I cannot use the std::find(std::begin(vector),std::end(vector),a). Thank you all so much.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {

  int digit = 0;
  int randNo = 0;
  cin >> digit;       
  srand((unsigned)time(NULL));
  vector <int> randG(digit);
            for(int i = 0; i < randG.size(); i++){
                    randNo = rand() %10;
                    randG.at(i) = randNo;
            }
  }




Aucun commentaire:

Enregistrer un commentaire