0

I have a program that can generate a random integer every 1/10 second. Here is the code:

int main()
{
    ofstream myfile;



    int max;

    cout << "Max number: ";
    cin >> max;

    for (int i = 0; i < max; ++i)
    {
        myfile.open("test.txt",fstream::app);
        myfile << random_int() << "\n";
        myfile.close();

        this_thread::sleep_for(chrono::milliseconds(100));
    }


    return 0;
    }

int random_int()
{
    return rand() % 10;
}

Now the question is, I need to write a program that calculate then output the average in the same rate. If the output of the number generator is:

1
2
3
4
5

The output of the average calculator should be

1
1
2
2
3

Every 1/10 second the program will output a number.

Note: The max number could be from 0 to couple millions. Calculating the average by adding all previous number during the time interval won't be ideal.

I am a sophomore student and a research assistant in a university. This is a simplified version of a problem that I encounter currently. Any suggestion will be greatly appreciated.

Update: Thanks for the help from Fei Xiang and oklar. Yes, remember the previous sum is the only way to make the calculation in time. However, since the random generator output file is changing constantly and the new output is appended to the old outputs, I am not sure how to get the most current data efficiently.

  • The maximum value of a `long long` is at least 2^63-1. That's about 9.223372*10^18, or more than 9 quintillion. Is that not big enough for you? – eesiraed Mar 03 '19 at 01:39
  • Thanks for helping. The problem is not about the size of the value. Trying to calculate the average during 1/10 second by just summing up all previous number could be just too slow, since there could be a million numbers. The real data output I am dealing with contains much more complex data and I need to process them during this small time interval. – user11097063 Mar 03 '19 at 02:11
  • Why not just remember the previous sum and add on the last number? – eesiraed Mar 03 '19 at 04:24
  • Thanks. I updated the question to show where I am having trouble with. – user11097063 Mar 03 '19 at 18:01
  • Then your problem becomes how to efficiently read the last number that has been appended to a file, for which there are already [solutions](https://stackoverflow.com/q/11876290/9254539) out there. – eesiraed Mar 04 '19 at 00:31
  • Thanks, this might work. I'll give it a shot. – user11097063 Mar 04 '19 at 03:05

1 Answers1

0

You don't need to calculate all the numbers that has been added each time, you only need to add the last one to a sum variable and divide by the amount of generated numbers.

Say you have:

1
2
3
4
5

Sum variable is 15. If you divide by the amount of numbers which is 5, you'll get the expected output of 3. Continuing, add the number 9 for instance to the sum variable and divide by the amount of generated numbers 6, you'll end up with an average of 4.

The i in your for loop can be used as a counter for the amount of generated numbers. Pseudo code:

sum += randomInt();
avg = sum/i;

EDIT: I see that you are opening and closing the file each time in the for loop in your post. This can be done outside the loop, which will speed things up. If I understand you correctly, your mission is to generate a random number then calculate the average from the previous numbers and finally append it to the text file? If so, you're on point.

int i_random;
int avg;
int sum = 0;
myfile.open("avg.txt",fstream::app);
for (int i = 1; i < max + 1; ++i)
{
    i_random = random_int();
    sum += i_random;
    avg = sum/i;
    myfile << avg << "\n";
    this_thread::sleep_for(chrono::milliseconds(100));
}
myfile.close();

See http://www.cplusplus.com/doc/tutorial/files/ for other operators. Check out cppreference for seek and tell if you want to skip to a position in the file.

ok4
  • 28
  • 6
  • Thanks for helping. I thought about that but I have problem to get correct value in time. Since random generator new output is always appended to the old output, the new program needs to skip all the old output to get the most current one. I have trouble about skipping the old data. – user11097063 Mar 03 '19 at 17:44
  • Do elaborate more. – ok4 Mar 03 '19 at 19:26
  • For example, right after 1/10 second, the output from random number generator is 1 2. How should I write a program to only read the last value without processing the previous one. By processing I mean such as "int garbage; myfile>>garbage;". Because it will be too slow. – user11097063 Mar 03 '19 at 20:37
  • Updated my original post. – ok4 Mar 03 '19 at 22:48
  • The random number generator is just a program I wrote to simulate the actual data that I am dealing with. I am trying to find a way to process that data during that small time interval, since the new data coming out every 1/10 second. Fei Xiang provided a method that might work. I will give it a shot. If I am stuck again, I'll come back. I really appreciate your help. – user11097063 Mar 04 '19 at 03:05