【问题标题】:How to generate mxn matrix with randomly generated 0 and 1 with probability in C如何在 C 中随机生成 0 和 1 的概率生成 mxn 矩阵
【发布时间】:2019-02-01 13:49:14
【问题描述】:

我编写了一个 C 程序,该程序定义了一个具有 m 行和 n 列随机数(0 或 1)的 2D 矩阵。代码如下:

int i,j;
    int original_matrix[m][n];
    for (i=0; i<=m-1; i++){
        for (j=0; j<=n-1; j++){
            original_matrix[i][j] = rand() % 2;
        }
    }

成功了。对于下一步,我想创建具有概率的矩阵。例如,1 以概率 p 写入单元格,0 以概率 1-p 写入。如果您有这方面的想法,请您分享一下吗?

【问题讨论】:

    标签: c random probability


    【解决方案1】:

    rand() % 2 给出的概率为 0.5。

    p 是一个浮点数,因此您将查看How to generate random float number in C 以生成一个真实范围内的随机值。最佳答案给了我们:float x = (float)rand()/(float)(RAND_MAX/a);

    我们希望a 的概率等于 1。因此,要以p 的概率得到 0,公式为:

    int zeroWithAProbabilityOfP = (float)rand()/(float)RAND_MAX <= p;
    

    也可以这样写:

    int zeroWithAProbabilityOfP = rand() <= p * RAND_MAX;
    

    ps:如果可以的话,出于精确的原因,你应该支持arc4random() or arc4random_buf()而不是rand()

    • rand() 精度为 1 / 0x7FFFFFFF(在 macOS 上)
    • arc4random() 精度为 1 / 0xFFFFFFFF(好两倍)

    在这种情况下,公式为:

    int zeroWithAProbabilityOfP = arc4random() <= p * UINT32_MAX;
    

    【讨论】:

      【解决方案2】:

      由于rand() 为您提供介于0RAND_MAX 之间的值,您只需选择适当的阈值即可获得特定百分比的值。例如,如果 RAND_MAX999,则预计所有值的 42% 将小于 420

      因此您可以使用以下完整程序中的代码来设置适当的阈值并测试您的值的分布:

      #include <stdio.h>
      #include <stdlib.h>
      #include <math.h>
      #include <time.h>
      
      int main(int argc, char *argv[]) {
          // Get threshold (defaults to ~PI%), seed random numbers.
      
          double percent = (argc > 1) ? atof(argv[1]) : .0314159;
          int threshold = round(RAND_MAX * percent);
          srand(time(0));
      
          // Work out distribution (millions of samples).
      
          int below = 0, total = 0;
          for (int i = 0 ; i < 1000000; ++i) {
              ++total;
              if (rand() < threshold) ++below;
          }
      
          // Output stats.
      
          printf("Using probability of %f, below was %d / %d, %f%%\n",
              percent, below, total, below * 100.0 / total);
      }
      

      一些样本运行,具有不同的期望概率:

      Using probability of 0.031416, below was 31276 / 1000000, 3.127600%
      Using probability of 0.031416, below was 31521 / 1000000, 3.152100%
      
      Using probability of 0.421230, below was 420936 / 1000000, 42.093600%
      Using probability of 0.421230, below was 421634 / 1000000, 42.163400%
      
      Using probability of 0.175550, below was 175441 / 1000000, 17.544100%
      Using probability of 0.175550, below was 176031 / 1000000, 17.603100%
      
      Using probability of 0.980000, below was 979851 / 1000000, 97.985100%
      Using probability of 0.980000, below was 980032 / 1000000, 98.003200%
      
      Using probability of 0.000000, below was 0 / 1000000, 0.000000%
      Using probability of 1.000000, below was 1000000 / 1000000, 100.000000%
      

      因此,底线是:要满足您对概率为 pdouble 值)和概率为 1 - p 的零的愿望,您需要以下内容:

      srand(time(0));                               // done once, seed generator.
      int threshold = round(RAND_MAX * p);          // done once.
      int oneOrZero = (rand() < threshold) ? 1 : 0; // done for each cell.
      

      请记住rand() 的限制,(例如)概率0.00000000000.0000000001 之间的差异很可能不存在,除非RAND_MAX 大到足以产生影响。我怀疑你是否会使用这么好的概率,但我想我最好提一下以防万一。

      【讨论】:

        猜你喜欢
        • 2020-05-24
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多