如何在实体中随机分配一定百分比的数字

时间:2019-01-07 11:31:52

标签: c++

我想按一定百分比在一个矩阵内随机分配数字,例如我有一个150 * 150的矩阵,我想用0,1,2填充 我随机地使用这样的百分比,例如10%表示0、30%表示2、60%表示1.我应该怎么办。实际上我做了一些事但没有百分比,但是效果并不理想。

for (int i = 0; i < 151 i++) {

    for (int j = 0; j <151; j++) {

        if (random(100) < 10) {

            Array(i, j) = 1;
        }

        if (random(50) < 10) {

            Array(i, j) = 2;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

自C ++ 11起,标准库提供了在标题<random>中定义的函数std::discrete_distribution

  

在区间[0,n)上产生随机整数,其中每个单独整数i的概率定义为w   i / S,即第i个整数的权重除以所有n个权重之和。

给出OP的百分比:

std::discrete_distribution<int> d({10, 60, 30});

HERE,可测试的代码段。

答案 1 :(得分:0)

我非常确定这不是解决此问题的最有效方法,并且绝不应该使用我的初学者c ++代码,但是如果您要引用,这也是我的方法:

#include <iostream>
#include <random>
#include <tuple>
#include <vector>

#define ROWS 5
#define COLS 5

using tuple = std::tuple<int, int>;

const int Percentage(const int value)
{
  const int percent = std::round((value / 100.0) * (ROWS * COLS));
  std::cout << value << "% of " << ROWS * COLS << " : " << percent << std::endl;
  return percent;
}

const int RandomIndex(const int& size)
{
  std::mt19937 range;
  range.seed(std::random_device()());
  std::uniform_int_distribution<std::mt19937::result_type> dist(0, size);
  return dist(range);
}

void FillMatrix(int matr[][COLS], std::vector<tuple>& num)
{
  // holds the numbers, from which a random number
  // will be stored to the matrix
  std::vector<int> fillers;

  // holds the random index among the fillers
  uint8_t random_index;

  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
      /*
       * check if the count of a particular number to be added to
       * the matrix is zero or not.
       * if zero : then dont append to filler vector
       * else    : append to filler vector
       */
      for (tuple item : num) {
        if (std::get<1>(item) != 0) {
          fillers.emplace_back(std::get<0>(item));
        }
      }

      // get the index of a random item in fillers vector
      random_index = RandomIndex(fillers.size() - 1);

      // insert this random element to matrix
      matr[i][j] = fillers[random_index];

      /*
       * find the percentage value(or count) of the number
       * corresponding to the random number and decrement it
       * so as to denote that it has been used.
       */
      for (tuple& item : num) {
        if (std::get<0>(item) == fillers[random_index]) {
          std::get<1>(item) -= 1;
        }
      }

      // clear the current fillers vector
      fillers.clear();
    }
  }
}

int main()
{
  int matrix[ROWS][COLS];

  // each tuple has a number and it's corresponding percentage
  std::vector<tuple> numbers = {tuple(0, Percentage(10)),
                                tuple(1, Percentage(30)),
                                tuple(2, Percentage(60))};

  // fill the matrix with values provided in the vector
  FillMatrix(matrix, numbers);

  // print the matrix
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
      std::cout << matrix[i][j] << "\t";
    }
    std::cout << "\n";
  }
  return 0;
}  

根据您的情况将ROWS和COLS定义为150。