-1

I have this code but it gives the same random numbers every time in all compilers so it must be wrong!

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
    ofstream output;
    output.open("Exercise.txt");   
    srand(time(0));
    int numbers = rand() % 1001;
    for(int i=0;i<100;i++) {
        output<< numbers << " ";
    }
    cout<<"Data appended"<<endl;
    return 0; //Return the value
}
mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33
exo444
  • 1
  • 2
  • If you are seeding random with time it shouldn’t be the same every time…. Are you running in some fake environment? Anyway rand is usually a very crappy prng… – Grady Player Nov 07 '21 at 05:05
  • 2
    Do you mean that the number is same every time you _execute_ the program, or that the same random number is repeatedly written to the file when you run it? (The second one is true) – mediocrevegetable1 Nov 07 '21 at 05:08
  • Perhaps similar to the example provided with [std::uniform_int_distribution](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution)? Or, using the [std::mersenne_twister_engine](https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine) directly? – David C. Rankin Nov 07 '21 at 05:15
  • 1
    Closing this as a typo: the problem is that you only generate one random number by a single call to `rand`. You need to call `rand` inside of the `for` loop, not outside of it. Not sure if you're used to a vectorized language like MATLAB, since you named the variable `numbers` like you think it contains a vector of numbers. It doesn't; `rand` only returns a single number. So the simple fix to this code is to move line 10 down into the `for` loop. Better though is to avoid using `rand` and `srand` in modern C++ in favor of the C++11 random tools: https://stackoverflow.com/a/13445752 – Cody Gray - on strike Nov 07 '21 at 05:45

3 Answers3

2

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:

  1. open file
  2. seed random
  3. generate number
  4. 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:

  1. open file
  2. seed random
  3. Repeat steps 4-5 1 hundred times
  4. Generate number
  5. 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
}
Kneecaps
  • 92
  • 8
1

You generate your random number once and then write it 100 times to the file.

If you move the generation inside the for loop, it should give what you expect.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
0

You can use the following program as a reference(which shows how to use loop) to write numbers into output.txt file:

#include <iostream> 
#include <fstream> 

int main()
{
    std::ofstream outputFile("output.txt");
    
    for(int i = 0 ; i < 100; ++i)
    {
        outputFile << (rand()%100)+1  << " ";
    }
    outputFile.close();
}

Also take a look at How to generate a random number in C++?.

Jason
  • 36,170
  • 5
  • 26
  • 60