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);
}

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

```