简体   繁体   中英

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). I am not sure if I am using rand( ) in the correct way, but this is what I have so far:

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. If you want to return an array of things in C, you have to make the caller pass it in (see below). 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). 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. The array integer is local to the function and will not be available outside. In either case, your function is supposed to return int and not an array.

And if you want to limit the random number to a range, you are better off using the mod operator such as 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. In case you are wondering about malloc & free, you can read here: http://www.codingunit.com/c-tutorial-the-functions-malloc-and-free

You could define your choose_N() like this

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. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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