# Challenge #2: My solution to the dice game

 Shamir Alavi · August 29, 2016 Here's my code for the dice game. Please let me know if you find any bugs. Suggestions/improvements on the code and my coding style is very welcome.``/* © Shamir Alavi, August 30, 2016 */#include #include #include #include #include #include /* The problem statement is to ask the user to roll three dice, calculate the sum   and ask her/him to guess if the sum of the next rolls would be higher, lower or   the same. *//* This program rolls a die thrice which is somewhat similar to rolling three dice   at the same time, at least within the scope of this program and what has been   taught so far. *//*not using functions (not taught yet) */int main(){    int diceRoll;    int sum = 0;    int previousSum;    //store sum from 1st iteration of rolls    int i;    char guess;    printf("Press any key to roll the dice three times.\n\n");    getchar();  // Can we do this with gets() or scanf()?    // 1st time rolling the dice    srand(time(0));    // randomize seed    for(i = 0; i < 3; i++){        diceRoll = (rand() %6) + 1;        printf("Roll # %d = %d\n", i, diceRoll);        sum += diceRoll;    }    printf("The sum of your dice rolls is %d\n\n", sum);    printf("Would your next sum be higher or lower or the same? (h/l/s)\n\n");    scanf(" %c", &guess);    printf("Your guess is '%c'\n", guess);    printf("Okay, let's roll the dice three times again.\n\n");    getchar();    // 2nd time rolling the dice    previousSum = sum;    sum = 0;    srand(time(0));    for(i = 0; i < 3; i++){        diceRoll = (rand() %6) + 1;        printf("Roll # %d = %d\n", i, diceRoll);        sum += diceRoll;    }    printf("The sum of your dice rolls is %d\n", sum);    // compare user's guess with ground truth    if((sum > previousSum && guess == 'h') || (sum == previousSum && guess == 's') || (sum < previousSum && guess == 'l')){        printf("\nYour guess is correct. You're awesome!\n\n");    }else{        printf("\nYour guess is incorrect. You're dumb.\n\n");    }    return 0;}``

## Replies

 Corey Ryan · August 30, 2016 Seems perfect. Only thing i can think to add to it this header file:``#include ``Along with:``srand(time(0));``So that every time that you run the code the numbers generated are different and random works perfectly.Hope this helped! Shamir Alavi · September 2, 2016 Thanks! I was looking for that function. Updated the code. Deepak Gautam · September 3, 2016 Hello Shamir,Can you explain why have you used the statement in your code. I mean what does the function srand() and time()  do? And why do we need it in this code? I think those functions were not taught in the videos before this challenge was given. Shamir Alavi · September 4, 2016 Hi Deepak,Thank you for asking. Yes, I'll happily explain the use of srand(time(0)) in this code.If you run the srand() function a few times and print the outputs, you'll see that even if it displays random numbers, there is still a pattern that the rand() algorithm follows every time you run it. This is because computers are incapable of generating truly random numbers unlike humans. They must follow a certain algorithm. This is why the generated numbers are also called pseudo-random numbers.Now, srand() is also a function that generates pseudo-random numbers but it can take a parameter called seed. For example, in srand(7), the value of the seed is 7. This seed can manipulate the randomness. Different seeds will give you different sets of random numbers. However, the problem does not end here. Unless you randomize the seed, you'll still get the same set of random numbers, just a different set every time you change it. Therefore, we need something that can truly randomize the seed parameter and thereby randomize the numbers every time we run srand(). This is where time() comes into the picture.time(0) or time(NULL) reads the current system time and returns the elapsed time in seconds since Jan 1, 1970. This is also called time since the EPOCH. As a result, the return value will be different every time you use it. While this isn't still truly random, this does the work for us to a great extent. Using time(0) as the seed for srand() will generate different sets of random number every time. That is why we use srand(time(0)) to generate random numbers. In this code, it helps us generate random rolls for the dice.Hope this helps!Cheers,Shamir yuenbing ooi · September 10, 2016 cool program!! Nisan Khanal · September 22, 2016 why the heck my program is generating same random numbers again and again..``#include #include #include #include int main(){    int roll1, roll2, roll3, total1, total2;    char inputCheck, check;    roll1 = (rand()%6) + 1;    roll2 = ((rand()%6) + 1);    roll3 = ((rand()%6) + 1);    total1 = roll1 + roll2 + roll3;    printf("the total of three dice rolls is %d \n", total1);    printf("Ok. guess whether the upcoming number will be higher or lower?? (H/L)");    scanf(" %c", &inputCheck);    check = toupper(inputCheck);    roll1 = ((rand()%6) + 1);    roll2 = ((rand()%6) + 1);    roll3 = ((rand()%6) + 1);    total2 = roll1 + roll2 + roll3;    printf("The total of another 3 dice rolls is: %d\n", total2);    if(total2 > total1){ check = 'H' ;} else{check = 'L';}    if(check = inputCheck){        printf("Good Job");    }else if( inputCheck != 'H' && inputCheck != 'L' && inputCheck != 'h' && inputCheck != 'l' ){            printf("input only 'H' of 'L', you dumb shit.\n");        }else{            printf("You suck");        }    return 0;}`` PANAGIOTIS MPAKOLAS · September 22, 2016 Hello!Great code!Is there some way not to use the srand(time(0)) ?Thank you! Taras Samoilenko · September 28, 2016 HiI am having troubles with challenge ?2. Can somebody take a look and help me?``#include #include int main(){ int i, x = 0, t=0; int diceroll, diceroll_re; int letter; for (i = 0; i<3; i++) { diceroll = (rand() % 6) + 1; x += diceroll; } printf("\n Total score of dice = %d", x); printf("\n Enter h for higher score than previous\n e for equal score than previous\n s for smaller score than previous\n"); scanf("%c", &letter); for (i = 0; i < 3; i++) { diceroll_re = (rand() % 6) + 1; t += diceroll_re; } if (letter == 104 || letter == 72 && diceroll_re>diceroll) { puts("You're right"); if (letter == 115 || letter == 83 && diceroll_re>diceroll) { puts("You're right"); if (letter == 101 || letter == 69 && diceroll_re>diceroll) puts("You're right"); else puts("You're really bad with guessing bub");  } } return 0;}`` Filip Kendeš · January 19, 2017 I didnt completely understand the challange but this is my code. Could you take a look at it and say what you think of it. Criticize everything so I can improve it later (Sorry for spelling mistakes)``#include #include #include #include #include int main(){    int counter;    int rollDice1;    int rollDice2;    int sum1 = 0;    int sum2 = 0;    int userChoice;    printf("\tROLL DICE GAME\n");    for(counter = 0; counter < 3; counter++)    {        rollDice1 = rand()%6 + 1;        printf("%d \n", rollDice1);        sum1 += rollDice1;    }    printf("sum of first three rolls is %d \n\n", sum1);    printf("Choose the sum of the next three rolls\n");    printf("Will it be: \n1. higher \n2. lower \n3. same____:");    scanf(" %d", &userChoice);    printf("\n"); // just a blank space    switch (userChoice)    {    case 1:        {            for(counter = 3; counter <6; counter++)            {                rollDice2 = rand()%6 +1;                printf("%d \n", rollDice2);                sum2 += rollDice2;            }            printf("sum of second three rolls is %d \n", sum2);            if (sum2 > sum1)                printf("\nYou won");            else                printf("You failed");            break;        }    case 2:        {            for(counter = 3; counter <6; counter++)            {                rollDice2 = rand()%6 +1;                printf("%d \n", rollDice2);                sum2 += rollDice2;            }            printf("sum of second three rolls is %d \n", sum2);            if (sum2 > sum1)                printf("You failed");            else                printf("You won");            break;        }    case 3:        {            for(counter = 3; counter <6; counter++)            {                rollDice2 = rand()%6 +1;                printf("%d \n", rollDice2);                sum2 += rollDice2;            }            printf("sum of second three rolls is %d \n", sum2);            if (sum2 == sum1)                printf("You won");            else                printf("You failed");            break;        }    }    return 0;}`` Em Pain · April 23, 2017 Brilliant! I was wondering how to randomize the randomizer, i kept getting the same results!
• 1

## C

126,770 followers