dimanche 19 novembre 2017

creating multiple objects with array of structures having unique random numbers

I am trying to create different objects with an array of a structure having random numbers in them. After compiling I am getting the same sequence of number in arrays of every object. Is there a possible way from which I can create different object having a unique sequence of numbers inside the array every time.

    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    using namespace std;

    struct storeTwoValue
    {
            int x;
            int y;
    };


    class practice{
    public:
    storeTwoValue storageArray[10];
    void valueGenerator()
    {       srand(time(NULL));
            for (int i = 0; i< 10; i++)
            {
                    storageArray[i].x = rand()%10 +1;
                            storageArray[i].y = rand()%7 + 1;
                    }
            }

            void print()
            {
                    cout<<"x"<<"    "<<"y"<<endl;
                    for (int i = 0; i< 10; i++)
                    {
                            cout<<storageArray[i].x <<"     ";
                            cout<< storageArray[i].y << endl;
                    }
                    cout<<endl;

            }
    };

      int main()
    {
            for(int i=0; i<3; i++)
            {       practice A;
                    A.valueGenerator();
                    A.print();
            }
            return 0;
    }




Aucun commentaire:

Enregistrer un commentaire