Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am trying to make this code to have something similar to this after run:

**************************************************** ************
FIFO simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 1 context switches.
**************************************************** ************
RR - Round Robin simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 3 context switches.
**************************************************** ************
HRN simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 2 context switches.
**************************************************** ************
NMR simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 3 context switches.

But for some reason I am having this:
**************************************** ********************
FIFO simulation with32control loops.


What I have tried:

C++
/* Simulation of processor allocation strategies */
#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <iostream>
#include <io.h>
#include<windows.h>

using namespace std;


FILE *textfile; /* Pointer to the file used */
char regis[70]; /* File record used (for reading) */

class Proc{
public:

/* identif: process identification */

char identif;
/* end: process completion status */
/* 0: ready to run */
/* 1: interrupted for input / output */
/* 3: interrupted for time */
/* 2: done */

    int end;

/* count: total control cycles assigned to the process */

    int count;

/* pri: process priority */

    int pri;

/* level: identification of the sub-queue where the process is */

    int level;


  Proc()
    { identif = 0; end = 0; count = 0; pri = 0; level = 0;}; // Initialization of values.
};

class processes {
public:
    int cc; // Number of control cycles.
    int np; // Number of processes.
    char auxiden; // Auxiliary variable.
    int auxend; // Auxiliary variable.
    int auxcount; // Auxiliary variable.
    int auxpri; // Auxiliary variable.
    int auxlevel; // Auxiliary variable.
    int change; // Auxiliary variable.
    Proc p[50]; // Vector (list) of processes.
    void start(); // Initial actions.
    void enter_data(); // Process identification load.
    void show(); // Show list of processes.
    void fifo(); // Simulation according to FIFO strategy.
    void rrobin(); // Simulation according to RR (Round Robin) strategy.
    void hrn(); // Simulation according to HRN strategy (Maximum Response Ratio).
    void rnm(); // Simulation according to the RNM (Multiple Level Feedback) strategy.
    void end(); //Final Actions

};

void processes::start(){
    cout << "*******************************************************";
    cout << "\n";
    cout << "*Processor Allocation Strategies*" ;
    cout << "\n";
    cout << "*in concurrent execution. *";
    cout << "\n";
    cout << "*******************************************************";
    cout << "\n";
    cout << "\n";
    cout << "Enter the number of control cycles for the simulation.";
    cout << "\n";
    cout << "(Suggested 30-40 per 10 processes):";
    cin >> cc;
    cout << "\n";
    cout << "Enter the number of processes in the simulation.";
    cout << "\n";
    cout << "(The maximum allowed is 50):";
    cin >> np;
    cout << "\n";
    if (np > 50)
    {np=50;
        cout << "The number of processes has been limited to 50. \n";
        cout << "\n";
    }
/* Open the file used for recording */
    cout << "Open summary file 'PROCESOS.TXT' for recording. \n";
    cout << "(BIN folder of TC folder).\n";
    cout << "\n";
    if ((textfile = fopen("processes.txt" , "a" )) == NULL)
    {printf("Error opening for recording in file 'processes.txt' \n ");
        exit(0);
    }
}

void processes::enter_data()
{ char opc;
    cout << "\nIf you want to manually enter the process id \n";
    cout << "type: 's' , else: 'n' and the system will assume the ids. \n";
    cin >> opc;
    if (opc == 's' ) {
        for ( int i=0; i<np; i++)
        {cout <<"Enter the process ID p["<> p[i].identif;
        }
    }

    else {
        p[0].identif = 'a';
        p[1].identif = 'b';
        p[2].identif = 'c';
        p[3].identif = 'd';
        p[4].identif = 'e';
        p[5].identif = 'f';
        p[6].identif = 'g';
        p[7].identif = 'h';
        p[8].identif = 'i';
        p[9].identif = 'j';
        p[10].identif = 'k';
        p[11].identif = 'l';
        p[12].identif = 'm';
        p[13].identif = 'n';
        p[14].identif = 'o';
        p[15].identif = 'p';
        p[16].identif = 'q';
        p[17].identif = 'r';
        p[18].identif = 's';
        p[19].identif = 't';
        p[20].identif = 'u';
        p[21].identif = 'v';
        p[22].identif = 'w';
        p[23].identif = 'x';
        p[24].identif ='y';
        p[25].identif = 'z';
        p[26].identif ='1';
        p[27].identif = '2';
        p[28].identif ='3';
        p[29].identif ='4';
        p[30].identif ='5';
        p[31].identif ='6';
        p[32].identif = '7';
        p[33].identif = '8';
        p[34].identif = '9';
        p[35].identif = 'A';
        p[36].identif = 'B';
        p[37].identif = 'C';
        p[38].identif = 'D';
        p[39].identif = 'E';
        p[40].identif = 'F';
        p[41].identif = 'G';
        p[42].identif = 'H';
        p[43].identif = 'I';
        p[44].identif = 'J';
        p[45].identif = 'K';
        p[46].identif = 'L';
        p[47].identif = 'M';
        p[48].identif = 'N';
        p[49].identif ='O';
    }

    for ( int i=0; i<np; i++)
    {p[i].end = 0;
        p[i].count = 0;
        p[i].pri = 0;
        p[i].level = 0;
    }
}

void processes::show(){
    printf("\n");
    cout << "List of processes:" << "\n";
    cout << "********************" << "\n" ;
    cout << "Status: 0 -> Ready to run." << "\n";
    cout << "State: 1 -> Interrupted waiting for input / output." << "\n";
    cout << "Status: 2 -> Process terminated." << "\n";
    cout << "Status: 3 -> Interrupted for time (not applicable in FIFO or HRN)."
         << "\n";
    for (int i=0; i<np; i++)
    {cout << "Process p[" << i << "]:" << p[i].identif << "Status:" << p[i].end <<
          "Control cycles used:" << p[i].count << "\n";
        cout << "Process p[" << i << "]:" << p[i].identif << "Priority:" << p[i].pri <<
             "Level:" << p[i].level;
        cout << "\n";
    }
}

void processes::fifo(){
    auxiden = 0; auxend = 0; auxcount = 0; change = 0;
/* Write to the file used */
    fprintf(textfile,"%s\n","**************************************** ********************");
    fprintf(textfile,"%s","FIFO simulation with");
    fprintf(textfile,"%i",cc);
    fprintf(textfile,"%s\n","control loops.");
    cout << "\n";
    cout << "Process selection sequence for execution according to FIFO:";
    cout << "\n";
    cout << "************************************************ ****************";
    cout << "\n";
    cout << "Processes are served according to their order in the list of ready processes:";
    cout << "\n" << "\n";
    for (int j=0; j<cc; j++) // ***Perform cc control loops***
    { auxend = 0;
        for ( int m=0; m<np; m++)
        {auxend = auxend + p[m].end;}
        if (auxend == (np * 2))
        {
            cout << "\n" << "All processes have finished in" << j << "control cycles.\n";

/* Write to the file used */

            fprintf(textfile,"%s" , "All processes have ended in");
            fprintf(textfile,"%i" , j);
            fprintf(textfile,"%s\n", "control cycles.");
            j=cc;
            auxend = getch();
        }
        for (int i=0; i<np; i++)
        {if (p[i].end == 0)
            {
                cout << "Processor assigned to process:" << p[i].identif;
                cout << "\n";
                p[i].count = p[i].count + 1;
                p[i].end = rand() % 3;// Determine next state of the process.
                cout << "\n" << p[i].identif << p[i].end << "\n" ;
                if (p[i].end == 1)
                    cout << "Process" <<p[i].identif << "interrupted by input / output." <<"\n";
                else {if (p[i].end == 2)
                        cout << "Process" <<p[i].identif << "terminated." <<"\n";
                }
                if (p[i].end > 0)
                { // Context swap.
                    change = change + 1;
                    auxiden = p[i].identif;
                    auxend = p[i].end;
                    auxcount = p[i].count;
                    for ( int k=i; k<(np - 1); k++)
                    {p[k].identif = p[k+1].identif;
                        p[k].end = p[k+1].end; p[k].count = p[k+1].count;
                    }
                    p[(np - 1)].identif = auxiden;
                    p[(np - 1)].end = auxend;
                    p[(np - 1)].count = auxcount;
                }
                i = np;
                auxend = getch();
            }

            for ( int k=0; k<np; k++) // Determines whether to continue waiting for input / output.
            {if (p[k].end == 1) p[k].end = rand() % 2;
            }
        }
    }
    cout << "\n" << "***Occurred" << change << "context switches.***" <<
         "\n";

/* Write to the file used */
    fprintf(textfile,"%s","Simulated");
    fprintf(textfile,"%i",np);
    fprintf(textfile,"%s\n","concurrent processes.");
    fprintf(textfile,"%s","Occurred");
    fprintf(textfile,"%i", change);
    fprintf(textfile,"%s\n","context changes.");
    auxend = getch();
}
void processes::rrobin(){
    auxiden = 0; auxend = 0; auxcount = 0; change = 0;
/* Write to the file used */
    fprintf(textfile,"%s\n","**************************************** ********************");
    fprintf(textfile,"%s","RR Simulation - Round Robin with");
    fprintf(textfile,"%i", cc);
    fprintf(textfile,"%s\n","control loops.");
    cout << "\n";
    cout << "Process selection sequence for execution according to RR - Round Robin:";
    cout << "\n";
    cout << "************************************************ ********************************";
    cout << "\n";
    cout << "Processes are served according to their order in the ready list,\n";
    cout << "but they have a limited time (quantum) of the processor:";
    cout << "\n" << "\n";
    for ( int j=0; j<cc; j++) // ***Perform cc control loops***
    { auxend = 0;
        for ( int m=0; m<np; m++)
        {if (p[m].end != 2)
            { auxend = 1;
                m=np;
            }
        }
        if (auxend == 0)
        {
            cout << "\n" << "All processes have finished in" << j << "control cycles.\n";

            /* Write to the file used */
            fprintf(textfile,"%s","All processes have ended in");
            fprintf(textfile,"%i",j);
            fprintf(textfile,"%s\n" ,"control cycles.");
            j=cc;
            auxend = getch();
        }
        for ( int i=0; i<np; i++)

        {if (p[i].end == 0)
            {
                cout << "Processor assigned to process:" << p[i].identif;
                cout << "\n";
                p[i].count = p[i].count + 1;
                p[i].end = rand() % 4; // Determine next state of the process.
                cout << "\n" <<p[i].identif <<p[i].end << "\n";
                if (p[i].end == 0) p[i].end = 3;
                if (p[i].end == 1)
                    cout << "Process" <<p[i].identif << "interrupted by input / output." << "\n";
                else {if (p[i].end == 2)
                        cout << "Process" <<p[i].identif << "terminated." << "\n";
                    else {if (p[i].end == 3)
                            cout << "Process" <<p[i].identif << "interrupted for time." << "\n";
                    }
                }
                if (p[i].end >= 0)
                { // Context swap.
                    change = change + 1;
                    auxiden = p[i].identif;
                    auxend = p[i].end;
                    auxcount = p[i].count;
                    for ( int k=i; k<(np - 1); k++)
                    {p[k].identif = p[k+1].identif;
                        p[k].end = p[k+1].end;
                        p[k].count = p[k+1].count;
                    }
                    p[(np - 1)].identif = auxiden;
                    p[(np - 1)].end = auxend;
                    p[(np - 1)].count = auxcount;
                }
                i = np;
                auxend = getch();
            }
            for ( int k=0; k<np; k++) // Determines whether to continue waiting for input / output.
            {if (p[k].end == 1) p[k].end = rand() % 2;
            }
            for ( int l=0; l<np; l++) // Determines whether to continue waiting for time.
            {if (p[l].end == 3)
                { auxend = rand() % 4;
                    if (auxend == 1) auxend = 0;
                    else {if (auxend == 2) auxend = 3;}
                    p[l].end = auxend;
                }
            }

        }
    }
    cout << "\n" << "***Occurred" << change << "context switches.***" << "\n";
/* Write to the file used */
    fprintf(textfile,"%s","Simulated");
    fprintf(textfile,"%i",np);
    fprintf(textfile,"%s\n","concurrent processes.");
    fprintf(textfile,"%s","Occurred");
    fprintf(textfile,"%i", change);
    fprintf(textfile,"%s\n","context changes.");
    auxend = getch();
}
void processes::hrn(){
    auxiden = 0; auxend = 0; auxcount = 0; auxpri = 0; change = 0;
/* Write to the file used */
    fprintf(textfile,"%s\n", "**************************************** ********************");
    fprintf(textfile,"%s","HRN simulation with");
    fprintf(textfile,"%i", cc);
    fprintf(textfile,"%s\n","control loops.");
    cout << "\n";
    cout << "Process selection sequence for execution according to HRN:";
    cout << "\n";
    cout << "************************************************ ****************";
    cout << "\n";
    cout << "Processes are served according to their priority in the ready list;\n";
    cout << "priority depends on response relation: (TE + TS) / TS, where\n";
    cout << "TE = Waiting Time and TS = Service Time:";
    cout << "\n" << "\n";
    for (int j=0; j<cc; j++) // ***Perform cc control loops***
    { auxend = 0;
        for ( int m=0; m<np; m++)
        {auxend = auxend + p[m].end;}
        if (auxend == (np * 2))
        {
            cout << "\n" << "All processes have finished in" << j << "control cycles.\n";
/* Write to the file used */
            fprintf(textfile,"%s", "All processes have ended in");
            fprintf(textfile,"%i" , j);
            fprintf(textfile,"%s\n", "control cycles.");
            j=cc;
            auxend = getch();
        }
        if (j == 0)
        {for ( int z=0; z<np; z++)

            {p[z].count = 1;}
        }
        if (j < cc)
        {
            for ( int l=0; l<np; l++)
            {p[l].pri = (j / p[l].count);
            }
        }
        if (auxpri == 1)
        {
            for ( int s=0; s<np; s++)
            {for ( int t=s; t<(np - 1); t++)
                {if (p[t+1].pri > p[t].pri)
                    {auxiden = p[t].identif;
                        auxend = p[t].end;
                        auxcount = p[t].count;
                        auxpri = p[t].pri;
                        p[t].identif = p[t+1].identif;
                        p[t].end = p[t+1].end;
                        p[t].count = p[t+1].count;
                        p[t].pri = p[t+1].pri;
                        p[t+1].identif = auxiden;
                        p[t+1].end = auxend;
                        p[t+1].count = auxcount;
                        p[t+1].pri = auxpri;
                    }
                }
            }
        }
    for ( int i=0; i<np; i++)
    {if (p[i].end == 0)
        { auxpri = 0;
            cout << "Processor assigned to process:" << p[i].identif;
            cout << "\n";
            p[i].count = p[i].count + 1;
            p[i].end = rand() % 3; // Determine next state of the process.
            cout << "\n" <<p[i].identif <<p[i].end << "\n";
            if (p[i].end == 1)
                cout << "Process" <<p[i].identif << "interrupted by input / output." << "\n";
            else {if (p[i].end == 2)
                    cout << "Process" <<p[i].identif << "terminated." << "\n";
            }
            if (p[i].end > 0)
            { // Context swap.
                change = change + 1;
                auxiden = p[i].identif;
                auxend = p[i].end;
                auxcount = p[i].count;
                auxpri = p[i].pri;
                for ( int k=i; k<(np - 1); k++)
                {p[k].identif = p[k+1].identif;
                    p[k].end = p[k+1].end;
                    p[k].count = p[k+1].count;
                    p[k].pri = p[k+1].pri;
                }
                p[(np - 1)].identif = auxiden;
                p[(np - 1)].end = auxend;
                p[(np - 1)].count = auxcount;
                p[(np - 1)].pri = auxpri;
                auxpri = 1; // Indicates that there was a context exchange and the list of processes must be reordered according to priorities.
            }
            i = np;
            auxend = getch();
        }
        for ( int k=0; k<np; k++) // Determines whether to continue waiting for input / output.
        {if (p[k].end == 1) p[k].end = rand() % 2;
        }
    }
    }
    for ( int y=0; y<np; y++)
    {p[y].count = p[y].count - 1;}
    cout << "\n" <<"***<< change<< context changes occurred.***" << "\n";

/* Write to the file used */
    fprintf(textfile,"%s","Simulated");
    fprintf(textfile,"%i",np);
    fprintf(textfile,"%s\n","concurrent processes.");
    fprintf(textfile,"%s","Occurred");
    fprintf(textfile,"%i", change);
    fprintf(textfile,"%s\n","context changes.");
    auxend = getch();
}
void processes::rnm(){
    auxiden = 0; auxend = 0; auxcount = 0; auxpri = 0; auxlevel = 0; change = 0;
/* Write to the file used */
    fprintf(textfile,"%s\n", "**************************************** ********************");
    fprintf(textfile,"%s","RNM simulation with");
    fprintf(textfile,"%i", cc);
    fprintf(textfile,"%s\n" ,"control cycles.");
    cout << "\n";
    cout << "Process selection sequence for execution according to RNM:";
    cout << "\n";
    cout << "************************************************ ****************";
    cout << "\n";
    cout << "Processes are served according to their level in the ready list;\n";
    cout << "but they have a limited time (quantum) of the processor;\n";
    cout << "if they are interrupted by input / output they remain in the subqueue\n";
    cout << "of the level where they are, but if they are interrupted by time they go to\n";
    cout << "a higher level, which is served when there are no more ready processes\n";
    cout << "in the lower levels; hence the name of\n";
    cout << "Multi-Level Feedback:";
    cout << "\n" << "\n";
    for (int j=0; j<cc; j++) // ***Perform cc control loops***
    { auxend = 0;
        for ( int m=0; m<np; m++)
        {if (p[m].end != 2)
            { auxend = 1;
                m=np;
            }
        }
        if (auxend == 0)
        {
            cout << "\n" << "All processes have finished in" << j << "control cycles.\n";
/* Write to the file used */
            fprintf(textfile,"%s","All processes have ended in");
            fprintf(textfile,"%i",j);
            fprintf(textfile,"%s\n", "control cycles."); j=cc;
            auxend = getch();
        }
        if (auxpri == 1)
        {
            for ( int s=0; s<np; s++)
            {for ( int t=s; t<(np - 1); t++)
                {if (p[t+1].level < p[t].level)
                    {auxiden = p[t].identif;
                        auxend = p[t].end;
                        auxcount = p[t].count;
                        auxpri = p[t].pri;
                        auxlevel = p[t].level;
                        p[t].identif = p[t+1].identif;
                        p[t].end = p[t+1].end;
                        p[t].count = p[t+1].count;
                        p[t].pri = p[t+1].pri;
                        p[t].level = p[t+1].level;
                        p[t+1].identif = auxiden;
                        p[t+1].end = auxend;
                        p[t+1].count = auxcount;
                        p[t+1].pri = auxpri;
                        p[t+1].level = auxlevel;
                    }
                }
            }
        }
        for ( int i=0; i<np; i++)
        {if (p[i].end == 0)
            { auxpri = 0;
                cout << "Processor assigned to process:" << p[i].identif;
                cout << "\n";
                p[i].count = p[i].count + 1;
                p[i].end = rand() % 4;// Determine next state of the process.
                cout << "\n" <<p[i].identif <<p[i].end <<p[i].level << "\n";
                if (p[i].end == 0) p[i].end = 3;
                if (p[i].end == 1)

                    cout << "Process" <<p[i].identif << "interrupted by input / output." << "\n";
                else {if (p[i].end == 2)

                        cout << "Process" <<p[i].identif << "terminated." << "\n";
                    else {if (p[i].end == 3)

                            cout << "Process" <<p[i].identif << "interrupted for time." << "\n";
                        p[i].level = p[i].level + 1;
                    }
                }
                if (p[i].end > 0)
                { // Context swap.
                    change = change + 1;
                    auxiden = p[i].identif;
                    auxend = p[i].end;
                    auxcount = p[i].count;
                    auxpri = p[i].pri;
                    auxlevel = p[i].level;
                    for ( int k=i; k<(np - 1); k++)
                    {p[k].identif = p[k+1].identif;
                        p[k].end = p[k+1].end;
                        p[k].count = p[k+1].count;
                        p[k].pri = p[k+1].pri;
                        p[k].level = p[k+1].level;
                    }
                    p[(np - 1)].identif = auxiden;
                    p[(np - 1)].end = auxend;
                    p[(np - 1)].count = auxcount;
                    p[(np - 1)].pri = auxpri;
                    p[(np - 1)].level = auxlevel;
                    auxpri = 1; // Indicates that there was a context exchange and the list of processes must be reordered according to priorities.
                }
                i = np;
                auxend = getch();
            }
            for ( int k=0; k<np; k++) // Determines whether to continue waiting for input / output.
            {if (p[k].end == 1) p[k].end = rand() % 2;
            }
            for ( int l=0; l<np; l++) // Determines whether to continue waiting for time.
            {if (p[l].end == 3)
                { auxend = rand() % 4;
                    if (auxend == 1) auxend = 0;
                    else {if (auxend == 2) auxend = 3;}
                    p[l].end = auxend;
                }
            }
        }
    }
    cout << "\n" << "***Occurred" << change << "context switches.***" << "\n";
/* Write to the file used */
    fprintf(textfile,"%s", "Simulated");
    fprintf(textfile,"%i",np);
    fprintf(textfile,"%s\n","concurrent processes.");
    fprintf(textfile,"%s","Occurred");
    fprintf(textfile,"%i", change);
    fprintf(textfile,"%s\n","context changes.");
    auxend = getch();
}
void processes::end(){
/* Close the file used */
    cout << "\nClose summary file 'PROCESSES.TXT'.\n";
    cout << "It is suggested to view its content with Windows Notepad.exe.\n" ;
    fclose(textfile);
}
int main(){
/* Variables */
    processes p1;
/* Code */

    system("cls");
    p1.start();
    p1.enter_data();
    p1.show();
    p1.fifo();
    p1.show();
    p1.enter_data();
    p1.show();
    p1.rrobin();
    p1.show();
    p1.enter_data();
    p1.show();
    p1.hrn();
    p1.show();
    p1.enter_data();
    p1.show();
    p1.rnm();
    p1.show();
    p1.end();
    getch();
}
Posted
Updated 20-Aug-23 14:05pm
v2
Comments
Richard MacCutchan 21-Aug-23 4:07am    
You cannot seriously expect anyone here to analyse some 400 lines of code on your behalf. Remove the code that is not relevant to the problem, and add a proper detailed explanation of what the problem is. You should also decide whather you are writing C code or C++ and stick to one type only. The mix of languages you have just makes everything more of a mess.

As Richard stated, rather post the code that has relevance as to just dumping all of your code.

You need to change your 'fifo()', 'rrobin()', 'hrn()', and 'rnm()' functions to capture the number of context switches and modify your 'end()' function to close the file properly. As an example (use the same for your other functions as I am not going to post 400 lines of code or fixes), you can use -
C++
void processes::fifo(){
    //Your existing code...
    for (int j = 0; j < cc; j++) {
        //Your existing code...
        for (int i = 0; i < np; i++) {
            if (p[i].end > 0) {
                //Your existing code...
                if (p[i].end > 0) {
                    //Your existing code...
                    change++;
                    //Your existing code...
                }
                i = np;
            }
            //Your existing code...
        }
    }
    //Your existing code...
}

//Repeat the above changes for your rrobin(), hrn(), and rnm() functions...

void processes::end() {
    /*Close the file you used... */
    cout << "\nClose summary file 'PROCESSES.TXT'.\n";
    cout << "It is suggested to view its content with Windows Notepad.exe.\n";
    fclose(textfile);
}

int main() {
    //Your existing code...

    //Print collected results...
    cout << "**************************************************** ************" << endl;
    cout << "FIFO simulation with " << cc << " control cycles." << endl;
    cout << np << " concurrent processes have been simulated." << endl;
    cout << "There have been " << p1.change << " context switches." << endl;

    //Repeat the above code for rrobin(), hrn(), and rnm() simulations...

    //Your existing code...

    return 0;
}
 
Share this answer
 
I don't have a specific answer for you but I noticed you did not seed the PRNG. At the beginning of main you should do this :
C++
srand( time( NULL ) % RAND_MAX );
That will give you a different random sequence of numbers every time you run your program. If you want the same sequence of numbers every time then use a constant value for the seed like this :
C++
srand( 43 );
One other thing : in processes::enter_data(), this line doesn't look right :
{cout <<"Enter the process ID p["<> p[i].identif;
 
Share this answer
 
Comments
k5054 20-Aug-23 14:46pm    
"That will give you a different random sequence of numbers every time you run your program"
Yes, but only if you don't start the program multiple times in the same second. That's probably not an issue 99% of the time, but something the OP should be aware of. The "randomness" of the system rand() function is also of dubious quality. Again, for 99% of tasks, particularly homework and/or personal projects that don't need good quality randomness, then its probably sufficient. If to OP does need better random data, then the facilities provided in the <random> header may be more useful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900