rand() returns the same number each time the program is run

后端 未结 6 1676
自闭症患者
自闭症患者 2021-02-02 10:46

In this rather basic C++ code snippet involving random number generation:

include 
using namespace std;

int main() {
    cout << (rand() %         


        
6条回答
  •  伪装坚强ぢ
    2021-02-02 11:24

    You need to change the seed.

    int main() {
    
        srand(time(NULL));
        cout << (rand() % 101);
        return 0;
    }
    

    the srand seeding thing is true also for a c language code.


    See also: http://xkcd.com/221/

提交回复
热议问题