# Challenge #2 Completed

 Daniel Seflinger · December 27, 2014 ``#define _CRT_SECURE_NO_WARNINGS//Challenge #2#include #include int main(){ int diceA, diceB, diceC, dicetotal, dicetotal2; char guess; diceA = (rand() % 6) + 1; diceB = (rand() % 6) + 1; diceC = (rand() % 6) + 1; dicetotal = diceA + diceB + diceC; printf("%i\n", diceA); printf("%i\n", diceB); printf("%i\n", diceC); printf("%i\n", dicetotal); printf("Will the next roll be higher or lower?(h for higher, l for lower): "); scanf("%c", &guess); diceA = (rand() % 6) + 1; diceB = (rand() % 6) + 1; diceC = (rand() % 6) + 1; dicetotal2 = diceA + diceB + diceC; printf("%i\n", diceA); printf("%i\n", diceB); printf("%i\n", diceC); printf("%i\n", dicetotal2); if (dicetotal2 > dicetotal && guess == 'h'){ printf("Awesome!\n"); } else if (dicetotal2 > dicetotal && guess == 'l'){ printf("You suck!\n"); } else if (dicetotal2 < dicetotal && guess == 'l'){ printf("Awesome!\n"); } else if (dicetotal2 < dicetotal && guess == 'h'){ printf("You suck!\n"); }}``I'm sure there was a much more efficient way to do this, but please comment if you have any questions or suggestions!

## Replies

 c student · December 27, 2014 ``#include #include #include int main() {        int dice[5] = {0};    srand (time (NULL));        for (int n = 0; n < 3; n++) {        dice[n] = rand() % 6 + 1;        printf ("Die %d: %d\n", n+1, dice[n]);        dice[3] += dice[n];    }    printf ("Total: %d\n", dice[3]);        puts ("Will the next total be (h)igher or (l)ower?");    int c = getchar();        for (int n = 0; n < 3; n++) {        dice[n] = rand() % 6 + 1;        printf ("Die %d: %d\n", n+1, dice[n]);        dice[4] += dice[n];    }    printf ("Total: %d\n", dice[4]);        int answer = dice[3] < dice[4] ? 'h' : 'l';    c == answer ? puts ("Correct answer\n") : puts ("Wrong answer\n");        return 0;}``you mean something like this? Daniel Seflinger · December 28, 2014 Oh wow this is great! thanks so much! Hmmm alright I think I see what you did...You used the time function to make it so the dice roll is random EVERY time, and not just the first time. Would you mind explaining how that gets incorporated into the program? Also yeah these loops do seem much more efficient! c student · December 28, 2014 here is some pre-requisite knowledge:rand() is a psuedo-random number generator, meaning it acts like a real rng but isn't as it is calculated with a formula (google to know more).the srand() function provides a seed to the rand() function where time(NULL) is the seed (NULL just means you're not storing the time in memory).  time(NULL) is an integer which is calculated in seconds since a specific date and time (google to know more) which is then provided to seed.  once srand() has this, it will seed the rand() function each time it is called and since time is dynamic, it changes every time (meaning within the range of one second), otherwise, as you may have witnessed in your program, it will output the same numbers each time as rand() without a seed is automatically seeded by a static integer of 1. Nishant Tadvi · December 28, 2014 Try to use "Same" also in the code so you would get three different thing Daniel Seflinger · December 28, 2014 Alright yeah thanks so much! I think I get it, but just to clarrify...ok so I understand the point of the time function and srand and all that. So, when you use srand(time(NULL)) it changes any other rand() function thereafter for the program? Sorry if worded that very weirdly lol. And @Nishant Tadiv, I'm not sure what you're telling me? Nishant Tadvi · December 29, 2014 I am saying that as you use higher, lower in code in that way use same Daniel Seflinger · December 30, 2014 oh oh i see. yeah that's a good point
• 1

## C

130,029 followers