lundi 25 novembre 2019

C++ same random number [duplicate]

This question already has an answer here:

While creating 2 different random numbers, they are the same as each other every time.

void foo() {
        int YPos;
        int XPos;
        YPos = ranInt();
        cout << YPos << endl;
        XPos = ranInt();
        cout << XPos << endl;
}
int ranInt() {
        srand(time(NULL));
        int ranNum;
        ranNum = rand() % 4 + 1;
        return ranNum - 1;
}

This gives me an example output:

2

2

How would I go about making these numbers different from each other?




Aucun commentaire:

Enregistrer un commentaire