【问题标题】:issues randomly populating a 2d array of structure type in C在 C 中随机填充结构类型的二维数组
【发布时间】:2015-11-15 22:48:02
【问题描述】:

我正在尝试填充一个 20x20 矩阵,其中每个条目都是结构类型。我的目标是在这个二维数组上随机分配 100 只蚂蚁和 5 只涂鸦虫。即使我让它工作,我并不总是在矩阵中得到我需要的蚂蚁或涂鸦虫的数量。我添加了一个计数功能,以在每次运行程序时始终验证我有多少个,但我总是有点短。我试图通过在我的填充函数中使用do/while 循环来强制这些数字工作(100 只蚂蚁和 5 只涂鸦虫),尽管它不起作用。有人能发现我的逻辑在哪里失败了吗?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#define N 20

struct cellState {
    int emptyInt;
    int antInt;
    int dBInt;

    char emptyChar;
    char antChar;
    char dBChar;
};

struct cellState gridState[N][N];

// function to populate world
void pop_mtx(struct cellState gridState[N][N], int antsNeeded, int dBNeeded) {
    int i, j;

    do {
        for (i = 0; i < N; i++) {    
            for (j = 0; j < N; j++) {
                if ((gridState[i][j].emptyInt = rand() % 3) == 0) {
                    gridState[i][j].emptyChar = '.';
                } else
                if (((gridState[i][j].antInt = rand() % 3 == 1) && antsNeeded != 0)) {
                    gridState[i][j].antChar = 'a';
                    antsNeeded--;
                } else
                if (((gridState[i][j].dBInt = rand() % 3 == 2) && dBNeeded != 0)) {
                    gridState[i][j].dBChar = 'D';
                    dBNeeded--;
                }
            }
        }
    } while (dBNeeded != 0 && antsNeeded != 0);
}

//function to display current state of the world
void display_mtx(struct cellState gridState[N][N]) {
    int i, j;
    char charToDisplay;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            if (gridState[i][j].antChar == 'a')
                charToDisplay = 'a';
            else
            if (gridState[i][j].dBChar == 'D')
                charToDisplay = 'D';
            else
                charToDisplay = '.';

            printf("%c  ", charToDisplay);
        }
        printf("\n");
    }
    printf("\n\n");
}

//function to count ants and doodlebugs
void count_mtx(struct cellState gridState[N][N]) {
    int i, j, antCount = 0, dBcount = 0;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            if (gridState[i][j].antChar == 'a')
                antCount++;
            else
            if (gridState[i][j].dBChar == 'D')
                dBcount++;
        }
    }
    printf("ant count: %i, doodlebug count: %i\n", antCount, dBcount);
}

int main(void) {
    srand((unsigned int)time(NULL));

    //populate grid state with 5 doodlebugs and 100 ants
    int antsNeeded = 100, dBNeeded = 5;
    pop_mtx(gridState, antsNeeded, dBNeeded);

    count_mtx(gridState);
    display_mtx(gridState);
}

【问题讨论】:

  • 您的人口逻辑看起来不正确。有几个问题,但主要的一个是一旦一个条目被 Ant 或 DB 填充,代码中没有任何内容可以防止它稍后被其他东西覆盖(由于外部 do 循环)。从理论上讲,外循环可以是无限的,因为由于算法的随机性,无法保证退出条件会得到满足(实际上它会结束但仍然不是一个好的算法恕我直言)。
  • 所以在满足条件 (dBNeeded!=0 || antsNeeded!=0) 的情况下,我取出了 do while 并使用了 if 语句。我还使用 (continue;) 在 else-if 循环中添加了一个 else。这似乎解决了问题

标签: c multidimensional-array random


【解决方案1】:

有几个问题。首先,每次调用 rand() 时都会获得不同的值,因此这三个测试都可能没有通过。您应该调用一次 rand() 并保存该值。

其次,没有什么可以保证通过 rand() 的 NxN 调用,您将获得所需数量的 1 和 2。因此,外循环是必要的。您还应该在一次迭代到下一次迭代中保留已经填充的正方形,因为可能需要很长时间才能达到产生足够多的一和二的迭代。

第三,这种方法偏向于网格开头的方格。它不会为您提供 100 只蚂蚁和 5 只涂鸦虫在 400 个方格上的所有可能分布中的一种等概率。

以下是正确的做法:

将网格视为一维数组。首先按顺序用 100 只蚂蚁、5 只涂鸦虫和空白空间填充它。然后执行一个random shuffle的数组。

此过程将以相等的概率返回网格上蚂蚁和涂鸦虫的每个可能分布。

【讨论】:

    猜你喜欢
    • 2012-08-21
    • 2017-04-17
    • 2012-07-27
    • 2014-05-10
    • 1970-01-01
    • 2018-02-14
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多