This question already has an answer here:
- Generate different random numbers 6 answers
In the following code, (x,y) of objects a and b might be initialized to same pair of numbers, since it's a random process. How can i ensure that it won't happen?
#include<iostream>
#include<cstdlib>
using namespace std;
class Position{
int x,y;
public:
Position(){
x=rand()%10;
y=rand()%10;
}
int getX(){return x;}
int getY(){return y;}
};
main()
{
Position a,b;
cout<<a.getX()<<" "<<a.getY()<<endl;
cout<<b.getX()<<" "<<b.getY();
}
Aucun commentaire:
Enregistrer un commentaire