c++ - Generating 9 random numbers without repeating -


i have assignment wants ne make magic square program generate random numbers 1-9 , assign them 2d array, cannot figure out how generate random numbers don't repeat , wondering if can me it's c++.

in advance thank you!

#include <iostream> #include <vector> #include <cstdlib> #include <ctime>  // returns true if item in list, otherwise false bool checkinlist(std::vector<int> &list, int value) {     (int = 0; < list.size(); ++i)     {         if (list.at(i) == value)         {             return true;         }     }      return false; }  // min inclusive, max exclusive // make sure set new seed before using make results more random. (std::srand) int randomint(int min, int max)  {     return rand() % (max - min) + min; }  int main()  {     std::vector<int> list;     std::srand(std::time(0));      while (list.size() < 9)     {         int num = randomint(1, 10);           if (!checkinlist(list, num))         {             list.push_back(num);         }     }      (int = 0; < list.size(); ++i)     {         std::cout << list.at(i) << std::endl;     }      return 0; } 

i liked sabreitweiser's answer simplicity, doesn't asked for.

the code posted should along lines of you're looking for.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -