Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

c++ - Unique Random Number between 0 and 9

I am trying to generate a unique random number between 0 and 9. The same number cannot be generated twice and the function will be ran 9 time (until all the 9 numbers are used.) Here is the latest way I have been trying to do this:

int uniqueRandomInt(int x) {

    std::vector<int> usedRandoms;
    int random = x;

    //Iterate vector
    for (unsigned int i = 0; i < usedRandoms.size(); i++) {
        //if passed value is in vector
        if (random = usedRandoms[i]) {
            uniqueRandomInt(random);
        }
        else {
            //If unique rand found put into vector
            usedRandoms.push_back(random);
            return random;
        }
    }
}

Calling it in another function using:

cout << uniqueRandomInt(-1) << endl;

Result I am getting is:

17801152 (Changes every time the function is called)

Am I going about this totally wrong? I did try other ways but with no luck and this is where I'm currently at. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I prefer to use shuffle.

#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
#include <cassert>

class T455_t
{
private:
   // data
   std::vector<int> m_iVec ;

public:
   T455_t() {}

   int exec()
      {
         std::vector<int> iVec;

         gen10();

         for (int i=0; i<10; ++i)
         {
            int nxtRandom = uniqueRandomInt();
            std::cout << nxtRandom << std::endl;
         }
         return(0);
      }


private: // methods

   void gen10() // fills data attribute with 10 digits
      {
         for (int i=0; i<=9; ++i)
            m_iVec.push_back(i);

         std::random_device rd;
         std::mt19937_64 gen(rd());
         std::shuffle (m_iVec.begin(), m_iVec.end(), gen);
         // m_iVec now contains 10 unique numbers, 
         // range 0..9, in random order
      }

   int uniqueRandomInt()
      {
         assert(m_iVec.size());
         int retVal = m_iVec.back(); // gets last element in vector
         m_iVec.pop_back();          // removes last element
         return(retVal);
      }

}; // class T455_t


int main(int argc, char* argv[])
{
   setlocale(LC_ALL, "");
   std::ios::sync_with_stdio(false);

   std::chrono::high_resolution_clock::time_point  m_start_us =
      std::chrono::high_resolution_clock::now();

   int retVal = -1;
   {
      T455_t   t455;
      retVal = t455.exec();
   }

   std::chrono::microseconds  chrono_duration_us =
      std::chrono::duration_cast <std::chrono::microseconds>
      (std::chrono::high_resolution_clock::now() - m_start_us);

   std::cout << "  FINI   " << chrono_duration_us.count() 
             << " us" << std::endl;
   return(retVal);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...