Why do I always get the same sequence of random numbers with rand()?

前端 未结 12 2433
忘了有多久
忘了有多久 2020-11-21 06:06

This is the first time I\'m trying random numbers with C (I miss C#). Here is my code:

int i, j = 0;
for(i = 0; i <= 10; i++) {
    j = rand();
    printf         


        
12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 06:43

    This is from http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.13.html#rand:

    Declaration:

    void srand(unsigned int seed); 
    

    This function seeds the random number generator used by the function rand. Seeding srand with the same seed will cause rand to return the same sequence of pseudo-random numbers. If srand is not called, rand acts as if srand(1) has been called.

提交回复
热议问题