jeudi 8 novembre 2018

how to generate different set of random numbers for classes in c++ [duplicate]

This question already has an answer here:

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