0 到 9 之间的唯一随机数 [英] Unique Random Number between 0 and 9

查看:42
本文介绍了0 到 9 之间的唯一随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个介于 0 和 9 之间的唯一随机数.相同的数字不能生成两次,并且该函数将运行 9 次(直到所有 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;

我得到的结果是:

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.

推荐答案

我更喜欢使用 shuffle.

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);
}

这篇关于0 到 9 之间的唯一随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆