简体   繁体   English

使用rand()在C语言中生成整数的随机序列

[英]Using rand( ) to generate a random sequence of integers in C

I am trying to implement a function int choose_N(void) that generates and returns a random list of integers N (up to four integers). 我试图实现一个函数int choice_N(void),该函数生成并返回整数N(最多四个整数)的随机列表。 I am not sure if I am using rand( ) in the correct way, but this is what I have so far: 我不确定我是否以正确的方式使用rand(),但这是到目前为止的结果:

int choose_N(void)
{
  int x, integer[4];
  srand ((int) time(NULL));
  for (x=0; x<4; x++)
   integer[x]=rand();
  return integer;
}

Would looping it like this work? 像这样工作会循环吗? Is time initialized in the correct way? 时间是否以正确的方式初始化?

Two problems: most importantly, the array integer is local, and will go away when you return. 两个问题:最重要的是,数组integer是局部的,返回时将消失。 If you want to return an array of things in C, you have to make the caller pass it in (see below). 如果要在C中返回事物数组,则必须使调用者将其传递给它(请参见下文)。 Secondly, srand() should only be called once per program , so it doesn't belong inside a function like this--it should be in main or some similar initialization function, otherwise multiple calls to this function will be correlated (at worst, they might even be identical). 其次, srand()仅应在每个程序中调用一次,因此它不属于这样的函数内部-它应位于main或某些类似的初始化函数中,否则对该函数的多次调用将相互关联(最糟糕的是,他们甚至可能是相同的)。 So here's a better approach: 所以这是一个更好的方法:

void choose_n(int *out, int count) {
    for (int i = 0; i < count; i += 1) {
        out[i] = rand();
    }
}

int main(int argc, char *argv[]) {
    int results[4];

    srand(time(NULL));
    choose_n(results, 4);
}

Your loop is correct but your return statement will not work. 您的循环是正确的,但是您的return语句将不起作用。 The array integer is local to the function and will not be available outside. 数组integer是该函数的局部变量,在外部将不可用。 In either case, your function is supposed to return int and not an array. 无论哪种情况,您的函数都应该返回int而不是数组。

And if you want to limit the random number to a range, you are better off using the mod operator such as rand() % range . 而且,如果要将随机数限制在一个范围内,最好使用mod操作符,例如rand() % range

You can't return a pointer to a local variable from a function. 您不能从函数返回指向局部变量的指针。 See this post for the reason why? 为何看到此帖子? Can a local variable's memory be accessed outside its scope? 是否可以在其范围之外访问局部变量的内存?

You have to allocate the array on heap. 您必须在堆上分配数组。

Change it to: 更改为:

int choose_N(void)
{
  int x;
  int *integer = (int*)malloc(sizeof(int)*4);
  srand ((int) time(NULL));
  for (x=0; x<4; x++)
   integer[x]=rand();
  return integer;
}

Don't forget to free() the memory when you are done. 完成后,别忘了free()内存。 In case you are wondering about malloc & free, you can read here: http://www.codingunit.com/c-tutorial-the-functions-malloc-and-free 如果您想了解malloc和free,可以在这里阅读: http : //www.codingunit.com/c-tutorial-the-functions-malloc-and-free

You could define your choose_N() like this 您可以这样定义您的choose_N()

void choose_N(int integer[4])
{
    int x;
    srand ((int) time(NULL));
    for (x=0; x<4; x++)
        integer[x]=rand();
}

and use it likes this 像这样使用

int integer[4];
choose_N(integer);

About the random part: If you really want to feel the randomness of c rand() seeded by time(0), you should run rand() like 3 times before you use it for generating actual numbers. 关于随机部分:如果您真的想感受由time(0)播种的c rand()的随机性,则在将其用于生成实际数字之前,应运行rand()3次。 To justify that, try running this code few times one after another while printing the numbers. 为了证明这一点,请尝试在打印数字时一次又一次地运行此代码几次。 You will notice that first one will be almost the same. 您会注意到第一个几乎相同。 My out: 我的:

1023384829 832292180 1773676349 957893636 
1023401636 1114767429 1248842775 1942837294 
1023418443 1397242678 724009201 780297305 

So your srand should look like this: 因此,您的评分应如下所示:

  srand ((int) time(NULL));
  rand(); rand(); rand();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM