Question: I have collection like this:

```
{id:1, name: "fluffy", 
  toys: [{id: "mouse", colors: ['red', 'green']}, 
         {id: "yarn", colors: ['blue', 'green']]
},

{id:2, name: "rex", 
  toys: [{id: "bone", colors: ['gray']}]
}  

```

I want to see results like this:

```
[
  {id: 1, name:"fluffy", toyColors:["red", "green", "blue"]},
  {id: 2, name:"rex", toyColors:["gray"]} 
]

```

I tried something like this, but I got 'lists of lists', instead of one big list for toy colors. Also, I did not know how to select the pet name with a query like this.

```
db.pets.aggregate({$group:{_id: "$id", toyColors: {$addToSet: "$toys.colors"}}})

```

Answer: 
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`.