3

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;
}
LihO
  • 41,190
  • 11
  • 99
  • 167
Lyrk
  • 1,936
  • 4
  • 26
  • 48
  • What do you mean by "not working" ? – ziu Mar 22 '13 at 12:53
  • 1
    Your problem is one that many, *many* people encounter when first trying to use random number interfaces, and has been [asked over and over again on Stack Overflow](http://stackoverflow.com/search?q=[c]+srand+is%3Aquestion) (that search isn't all duplicates, but nearly half of them are). – dmckee --- ex-moderator kitten Mar 22 '13 at 13:00
  • When at first I put srand inside rollDice, it everytime returned 7 but when I put it under main, it gave me different results as expected. – Lyrk Mar 22 '13 at 13:12
  • How many rolls before the user starts accurately guessing the outcome? ;) – autistic Mar 22 '13 at 13:25

3 Answers3

9

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.

pmg
  • 106,608
  • 13
  • 126
  • 198
5

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.

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
1

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.

K Scott Piel
  • 4,320
  • 14
  • 19
  • But if it runs once when I put in main, why is it giving differenr results? I think because it is called one in main, it has to give same over and over. When I put it inside rollDice, because rollDice is called at different times, it has to give different numbers. But situation is not like this.??? – Lyrk Mar 22 '13 at 13:14
  • 1
    srand() "seeds" the random number generator. i.e... it sets up the pseudo-random sequence of numbers that rand() will later return to you. If you seed the random number generator multiple times with the same number, then you will always get the same result from rand(). If you call srand() inside of rollDice() and you pass in the current time, then you "restart" the random number generator with the exact same value every time until the time changes (the second hand ticks). When you call srand() in main, you only seed the generator once and every call to rand() after that returns a new result. – K Scott Piel Mar 22 '13 at 13:18