# Thread: Generating 1000 random integers

1. ## Generating 1000 random integers

This is a little snippet of code I wrote. Out of 1000 random integers I need to choose 50. This is what I wrote but it falls short. Any help?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

int x, y, ia[50];

for(x=0; x<50; x++) {
srand(time(NULL));
y=rand()%1000+1;
ia[x]=y;
printf("%d ", ia[x]);
}

return 0;

}```

2. Hello,

If I understand correctly, then I believe your syntax is wrong. You are creating a loop and looping through 50. If you wanted to create 1000 random numbers, wouldn't you loop through 1000 and pick 50 out the of 1000 after they are generated?

- Stack Overflow

3. It falls short, how exactly?

4. The problem with the code is that it generates 50 integers, but it is 50 of the same number.

5. Do you have to choose the 50 random numbers randomly?

You could store 1000 numbers in an array and then pick numbers from the array by stepping through the array by a random amount; making the maximum step such that you couldn't run out of array before you have 50 numbers (i.e. a maximum random step of 20?)

6. Oh, and seed rand outside your loop.

7. Originally Posted by Azuth
Oh, and seed rand outside your loop.
Bingo!! Got it. Thanks all!!!!

8. De Ja Vu, indeed!