Problems when calling srand(time(NULL)) inside rollDice function

若如初见. 提交于 2019-12-17 12:42:39

问题


When I used at first srand(time(NULL)) in rollDice() function it did not work. But when I put it in main, it works. Why is that? Can you tell me the logic?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int rollDice(void) {
    return (1+rand()%6) + (1+rand()%6);
}
int main(void) {
    int roll;
    srand(time(NULL));          
    roll = rollDice();
    printf("You rolled %d.\n", roll);

    enum Gamestatus {WON,LOST,CONTINUE};
    enum Gamestatus status;

    while(status==CONTINUE){
        printf("You are rolling again: \n");
        printf("You rolled %d\n", roll = rollDice());

        if (targetPoint==roll){
            printf("You win!");
            status=WON;
        }
        else if(7==roll){
            printf("You lost!");
            status=LOST;
        }
        else
            status=CONTINUE;
    }
    return 0;
}

回答1:


Let's say you have millions of books with rows upon rows of random numbers. Before you get a random number you need to select a book.

After you have a book, to get random numbers, read numbers sequentially from the book. Changing the book gets another sequence of random numbers.
Changing to the same book restarts the sequence form the first number in the book.

srand() chooses a book and starts random numbers from the beginning
rand() reads the next number from the selected book

If you put srand() inside the loop, you are effectively restarting the random number sequence from the beginning of the same book.

Solution: select 1 book once, and keep reading numbers from it for ever and ever.

In a C program, if you don't "select a book", the random numbers come from book #1 or, in other words, in the absence of a srand() call, the function rand() behaves as if srand(1) has been called.




回答2:


When you do the seeding within the rollDice function and then you're calling this function in a loop, not only that you are seeding more often than you should, but the loop is so fast that you are seeding with the same value (same time) which yields in rand() returning the same value:

Thesrand()function sets its argument as the seed for a new sequence of pseudo-random integers to be returned byrand().These sequences are repeatable by callingsrand()with the same seed value.

Rule of thumb: Seed only once.

Worth to have a look at previous questions addressing this problem:
srand(time(NULL)) doesn't change seed value quick enough
Always repeated numbers given by rand()
srand function is returning same values etc.




回答3:


The issue is that the srand() function is seeding the random number generator and you're seeding it with the current time, which has a 1 second resolution. So, when you call srand() from inside of the rollDICE() function, you get the exact same result from rand() on every call that occurs in the same second. When the second ticks, you'll get a different result from rand().

Calling srand() inside main means that you only call it once, before you start rolling, and then rand() will return you a sequence of random numbers.



来源:https://stackoverflow.com/questions/15570803/problems-when-calling-srandtimenull-inside-rolldice-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!