I'm trying to create 1000 random numbers using an array. I keep getting the same number everytime, though. How do I get 1000 different random numbers? Here's what I have so far:

[LIST=1]
[*]#include "stub.h"
[*]int main()
[*]{
[*]    int number[1000], i;
[*]    int r[1000];
[*]    
[*]    for(i=0; i<1000; i++)
[*]    {
[*]       r[i] = (rand() % 10) +1;
[*]       srand(time(NULL)); 
[*]       cout << " " << r[i] << " "; 
[*]     }   

[*]     getchar();
[*]    
[*]	return 0;
[*]}
[/LIST]

Recommended Answers

All 3 Replies

Ignore the int number[1000];

you have to call srand() to generate a new set of numbers - and its only called once at the beginning of the program.

#include <stime>
...
int main()
{
    srand( time(0) );

}

If you want those 1000 numbers to have a limit use this:

for (int i=0;i<arraySize;i++) {


a=1 + rand() % limit;
}
}

dont forget to use #include <cstdlib>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.