mercredi 1 septembre 2021

Why do I get the same number when using rand/srand in a function before main?

Why am I getting a different random number each time I run my code in main, but when I run it in a function I am getting a static number?

(1) Random number in Main...

int main()
{
    srand(time(0));
    int loot = 1+(rand()%9);
    cout << loot;
    return 0;
}

(2) Random Number in a function outside of main...

using namespace std;

void lootTable(){
    srand(time(0));
    int loot = 1+(rand()%9);
    cout << loot;
}

int main()
{

    cout << lootTable;
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire