【问题标题】:Loop within a loop & random generation in c循环内循环和c中的随机生成
【发布时间】:2013-06-18 14:52:25
【问题描述】:

我在这件事上挠头太久了。 我看到了很多关于如何在 C 循环中处理随机生成的主题,但是当涉及两个循环时我什么也没看到。

下面是我的代码:

typedef struct
{
    char qh_uid[6];
    long int qh_vd;
    long int qh_pd;
    long int qh_id;
    double qh_value;
}quote_history;

int records_size = 20;
int batch_size = 5;

char *rand_str(char *dst, int size)
{
    static const char text[] = "abcdefghijklmnopqrstuvwxyz"
                               "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i, len = size;
    for ( i = 0; i < len; ++i )
    {
        dst[i] = text[rand() % (sizeof text - 1)];
    }
    dst[i] = '\0';
    return dst;
 }

char *rand_tstp(char *dst, int size)
{
    static const char text[] = "0123456789";
    int i, len = size;
    for ( i = 0; i < len; ++i )
    {
        dst[i] = text[rand() % (sizeof text - 1)];
    }
    dst[i] = '\0';
    return dst;
}

double randfrom(double min, double max)
{
    double range = (max - min);
    double div = RAND_MAX / range;
    return min + (rand() / div);
}

quote_history *feed_batch(quote_history *qh_batch, int batchsize)
{
    int j;
    char mytext[6];
    char mylong[17];
    for (j = 0; j < batchsize; ++j)
    {
        quote_history qh_aux;
        printf("uid : %s - value : %lf\n",rand_str(mytext, sizeof mytext), randfrom(0,100000));
        strcpy(qh_aux.qh_uid, rand_str(mytext, sizeof mytext));
        qh_aux.qh_vd = strtol(rand_tstp(mylong, sizeof mylong), NULL, 10);
        qh_aux.qh_pd = strtol(rand_tstp(mylong, sizeof mylong), NULL, 10);
        qh_aux.qh_id = strtol(rand_tstp(mylong, sizeof mylong), NULL, 10);
        qh_aux.qh_value = randfrom(0,100000);
        qh_batch[j] = qh_aux;
    }
    printf("--------------\n");
    return qh_batch;
}

int
main(int           const argc,
     const char ** const argv) {

    quote_history *subArray;

    srand(time(NULL));
    int k;
    for (k = 0; k < (int)(records_size/batch_size); ++k) {
        subArray= (quote_history*)calloc(batch_size, sizeof(quote_history));
        subArray = feed_batch(subArray, batch_size);
        // Do something with subArray
        free(subArray);
    }
}

嗯,基本上,我想做的是生成 struct quote_history 的批次(作为数组),其中一些值是随机生成的,然后处理该批次并移至另一个批次。

由于某种原因,随机生成在 feed_batch 函数中运行良好,但当它移动到循环中的下一项时,批次始终保持不变。

忘记粘贴代码结果了:

uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------
uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------
uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------
uid : qJfzrJ - value : 64938.995598
uid : LyCadK - value : 23030.096583
uid : dcOicU - value : 26016.211568
uid : BmpSTV - value : 76145.000279
uid : aQvABq - value : 92286.726130
--------------

我尝试了很多组合(例如在循环中循环而不是 feed_batch 函数),但均未成功。

我希望有人能帮助我。

弗洛里安

【问题讨论】:

  • 这不是您的完整代码 - 例如,qh_aux 没有定义。
  • 这不会编译。什么是 qh_aux?
  • '\0'rand_str 编写的终止符将超出 mytext 数组的边界,就像您调用函数一样。小心!
  • 这可能与您的问题无关,但在rand_str() 中,您正在使用dst[i] = '\0'; 语句写入缓冲区末尾。
  • 抱歉,我拍摄了注释行,忘记复制 qh_aux 的声明。我已经编辑过了

标签: c for-loop random srand


【解决方案1】:

如果我理解您的代码,那么您缺少的是将随机生成的字符串复制到结构的数组 qh_uid 中。实际上你不需要复制,你可以直接在你的结构的缓冲区中生成随机字符串。

quote_history *feed_batch(quote_history *qh_batch, int batchsize)
{
    int j;
    for (j = 0; j < batchsize; ++j)
    {
        rand_str(qh_batch[j].qh_uid, sizeof(qh_batch[j].qh_uid) - 1);
        printf("uid : %s - value : %lf\n", qh_batch[j].qh_uid, randfrom(0,100000));
    }
    printf("--------------\n");
    return qh_batch;
}

Sander De Dycker 在评论中提到的也是真的,你不能写qh_uid[6],你可以写的最大值是qh_uid[5],(这样做,除了对齐考虑,你实际上覆盖qh_vd 的第一个字节),记住数组索引从0 开始。因此 -1 添加到传递给 rand_str 的大小。

还要考虑 vlad_tepesch 关于分配的回答。

【讨论】:

  • 一开始,我并没有在 printf 中重定向 random 的结果,我只是为了调试目的。我使用 strcpy 和 strtol 在我的结构中重定向随机生成的项目,您现在可以在代码中看到
  • 你为什么在qh_aux工作然后复制?为什么不直接处理要填充的内存?
  • 好吧,因为我没想到... :-(
  • 对于它的价值,直接替换填充到 qh_batch 也没有多大作用......我将看看 vlad_tepesch 正在谈论的无用的动态分配
  • 好吧,你的代码对我来说是有效的。你确定你正在编译正确的文件吗?在正确的目录中工作?或者还有什么像这样的傻事?
【解决方案2】:

你的 main 分配了一个 quote_history 字段并在 for 循环中释放它。

feed_batch 获取这个指针并像访问数组一样访问它——>您的调试器应该报告内存访问错误。

新猜测:
我知道为什么你认为它确实有效(printfs 是正确的)但在外面它不起作用。但这只是一个猜测,因为缺少代码: 我猜你将 mytext 指针保存在你的结构中。所以最后你的所有数组元素都指向同一个字符串。更糟糕的是,字符串是一个自动变量,在退出 feed_batch 后会失去作用域。

顺便说一句:
为什么需要动态分配? 只需声明一个 quote_history 对象并传递它的指针。 或声明一个 quote_history 对象数组。这里不需要动态分配。

【讨论】:

  • 它为batch_size(即5)quote_history对象分配空间,而不仅仅是1
  • 感谢您的输入,您是对的,正如您现在在代码中看到的那样,我将 mytext 变量保存在我的结构中。
猜你喜欢
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 2011-02-12
  • 1970-01-01
相关资源
最近更新 更多