help pleezzzzzzzze

1
2
3
4
5
6
7
8
9
10
11
    cin>>initMaxX>>initMaxY;
    
    int size=initMaxX*initMaxY;
    bug b[size];
    
    for (int i=0; i<size; i++)
    {

        b[i]=bug(initMaxX,initMaxY);
        cout<<b[i].getX()<<","<<b[i].getY()<<endl;
    }



^^ this above code is to store bug data in an array (a bug for each element).


the below code is the constructor definition


1
2
3
4
5
6
7
8
9
10
11
12
        bug::bug(int initMaxX=100, int initMaxY=100){
          
            srand((unsigned)time(NULL));
            maxX=initMaxX;
            maxY=initMaxY;
            x=0;
            y=0; 
            x=rand() % maxX+1;
            y=rand() % maxY+1;
            alive=true;

        }


the .getX() is a member function to return the x value of each instance of bug. The problem is: i keep getting the same "random" x and y variables for every single instance of the array after my for loop terminates. Does anyone know whats wrong?
Last edited on
You're reinitializing the random seed each time you call the constructor (line 3).
You then get eactly the same random numbers each time.
srand should only be called once in your program (usually in main).
thanks :b
Topic archived. No new replies allowed.