# Challenge 2

 Ruben L · October 14, 2015 I have a question. Why does my program pick the same numbers everytime i run it?#include #include #include int main(){   int diceRoll1;    int diceRoll2;    int diceRoll3;    int sum1;    char x;    char answer;    int sum2;    printf("Press x to roll 3 dices once.\n (You can press the letter e anytime to exit the game.)\n");    while(x != 'e'){        scanf("%s", &x);            if(x =='x'){                diceRoll1 = (rand()%6) +1;                printf("You got an %d\n", diceRoll1);                diceRoll2 = (rand()%6) +1;                printf("You got an %d\n", diceRoll2);                diceRoll3 = (rand()%6) +1;                printf("You got an %d\n", diceRoll3);                sum1 = diceRoll1 + diceRoll2 +diceRoll3;                printf("The sum of the 3 dice rolls is: %d\n", sum1);                printf("Do you think the next 3 rolls sum is gonna be higher, lower or the same? \n(answer with: h, l or s)\n");                    scanf("%s", &answer);                        diceRoll1 = (rand()%6) +1;                        printf("You got an %d\n", diceRoll1);                        diceRoll2 = (rand()%6) +1;                        printf("You got an %d\n", diceRoll2);                        diceRoll3 = (rand()%6) +1;                        printf("You got an %d\n", diceRoll3);                        sum2 = diceRoll1 + diceRoll2 +diceRoll3;                        printf("The sum of the 3 dice rolls is: %d\n", sum2);                            if(answer == 'h' && sum2 >sum1){                                printf("Good job dude.\n");                                printf("Press x to roll 3 dices once more.\n");                            }else if(answer == 'l' && sum2

## Replies

 Linguist Llama · October 15, 2015 A string is a sequence of characters that terminates at the first '\0' character. Even an empty string needs to have that string at the end, and we can test this:``printf("%zu\n", sizeof "");``We can see that the empty string has a size of 1 byte or character. Remember this; I'll come back to this point at a later time. If we print that character...``printf("%d\n", ""[0]);``... we can see that it has an integer value corresponding to 0, which is the same as '\0' and not to be confused with '0':``printf("%d is not %d!\n", '\0', '0');``I mentioned earlier that an empty string has a size of exactly one character, and that is the '\0' character that is required for it to be a string... so how many characters can you store in one char variable? Let us see...``char answer;printf("%zu\n", sizeof answer);``That's right! We can store exactly one byte or character into answer., and if we want answer to be a string, that can only be an empty string.If you expect a string to contain one character, you need one + one for the '\0':``char answer[2];printf("%zu is %zu\n", sizeof answer, sizeof "y");``So now the entirety of the string "y" will fit into answer!If you expect a string to contain four characters, you need four + one for the '\0' (six):``char answer[6];printf("%zu is %zu\n", sizeof answer, sizeof "four");`` So now the entirety of the string "four" will fit into answer...Do you understand the problem in your program yet? Ruben L · October 15, 2015 I have a question. Why does my program pick the same numbers everytime i run it ? Linguist Llama · October 16, 2015 That question is also known as "What is the purpose of srand(time(0))?" for those who are observant enough to notice that line of code in the examples they're shown.rand is designed to be random in the "even distribution" sense, meaning you have a deck of cards that's shuffled and once you pull the queen of spades, you know there's no other queen of spades. That's not unpredictable, is it? rand is an extremely predictable linear congruent generator, which means it starts with a seed and performs some mathematical operations on the seed. The result forms the next seed and what rand returns.According to the C standard, section 7.22.2.2 , the default initial seed value is 1.If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.If you want to reseed the linear congruent generator, that is done by calling srand with the seed that you desire. If you give it the same seed every time, of course you'll get the same sequence of numbers every time, hence the reason time(0) is given as the initial seed.If you want something unpredictable (perhaps even secure), don't use rand. Use a cryptographic random number generator, instead. They are designed to be unpredictable, and thus secure.
• 1

## C

129,883 followers