#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class A{
int x;
public:
void setX(){
srand( time(NULL));
x = rand() % 15;
}
int getX(){
return x;
}
};
int main()
{
A dizi[5];
for(int i = 0; i < 5;i++){
dizi[i].setX();
cout<<dizi[i].getX()<<endl;
}
}
or
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class A{
int x;
public:
void setX(){
srand( time(NULL));
x = rand() % 15;
}
int getX(){
cout<<x<<endl;
}
};
int main()
{
A dizi[3];
dizi[0].setX();
dizi[1].setX();
dizi[2].setX();
dizi[0].getX();
dizi[1].getX();
dizi[2].getX();
}
x is always printing the same value. How can I assign different random values to each object of the array.
I've tried srand(time (0)) and srand(time (NULL))
but they didn't work.
every class instance produces same random value, why?