To understand your problem you need to have a look at the order you are executing your code in, you gotta look at what you have done and follow it like the computer will when executing the code. The computer would try to do it in this order:
- open file
- seed random
- generate number
- Write number 100 times
The problem is you only generated the number once. So basically, you generated one random number, and then printed it 100 times. What your code should be doing is more like:
- open file
- seed random
- Repeat steps 4-5 1 hundred times
- Generate number
- Write number
To solve your problem, just put the assignment to the random number inside the for loop as well. Your code should look like what I have put under, so you seed the random, then inside the for loop, keep generating new numbers with each iteration.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int numbers;
ofstream output;
output.open("Exercise.txt");
srand(time(NULL));
for(int i=0;i<100;i++) {
numbers = rand() % 1001;
output<< numbers << " ";
}
cout<<"Data appended"<<endl;
return 0; //Return the value
}