dimanche 5 mai 2019

Why is this program outputting the same results 4 times?

I want this program to generate 4 different series of 4 random numbers. The program is currently outputting the same numbers for each series. Any idea what's going on? I assume it's related to the seed but I don't know exactly what.

#include <iostream>
#include <cstdlib>
#include <ctime> 

using namespace std;
int main()
{
    int lotArr[6];
    int j = 0;
    int k = 0;
    int m = 0;
    int n = 0;
    int i = 0;
    bool crit = false;

    //interates through 4 sequences
    for(int i = 0; i < 4; i++ )
    {
        srand(time(NULL));
        //generates each number in current sequence
        for(m = 0; m < 4; m++)
        {
            lotArr[m] = rand() % 30 + 1;
        }

        //output
        for(n = 0; n < 4; n++)
        {
            cout << lotArr[n] << " ";
        }
        cout << endl;
    }
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire