Tutorial 38 challenge

0 Akshay B P · November 8, 2014
program.cbp

Each time I run the program, the same number is being generated.
What should I do ?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
    int a,b;char x;
    int diceroll1,diceroll2,diceroll3;

    diceroll1=(rand()%6) + 1;
    diceroll2=(rand()%6) + 1;
    diceroll3=(rand()%6) + 1;
    a = diceroll1+diceroll2+diceroll3;
    printf("%d \n", a);

    printf("Guess the next turn (l,h,s) \n");
    scanf("%c",&x);

    diceroll1=(rand()%6) + 1;
    diceroll2=(rand()%6) + 1;
    diceroll3=(rand()%6) + 1;
    b = diceroll1+diceroll2+diceroll3;
    printf("%d \n", b);

    if(((a>b)&&(x=='l'))||((a<b)&&(x=='h'))||((a==b)&&(x=='s')))
        printf("You Win \n");
    else
        printf("Better luck next time \n");
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Brian Linaburg · December 5, 2014
I was having the same problem with the random numbers being the same each time also. Thanks Tyler for posting your code. I haven't learned about srand yet, so it helped out a lot. 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>  //thanks T.B.

int main()
{
    char guess;
    int i;
    int diceRoll;
    int total1 =0;
    int total2 =0;
   
    srand(time(0));  //thanks T.B.
   
    for(i=0; i < 3; i++){
        diceRoll = ( rand()%6)+1;
        printf("%d \t", diceRoll);
        total1 = total1+diceRoll;
    }
   
    printf("\nTotal first total is %d \n", total1);
    printf("Next round higher, lower, or same? (h/l/s) \n");
    scanf("%c", &guess);
   
    for (i=0; i < 3; i++) {
        diceRoll = ( rand()%6)+1;
        printf("%d \t", diceRoll);
        total2 = total2+diceRoll;
    }
   
    printf("\nThe second total is %d \n", total2);
   
    if (total1>total2) {
        if (guess == 'l') {
            printf("Winner \n");
        }else{
            printf("Try Again \n");
        }
    }
    if (total1<total2){
        if (guess == 'h') {
            printf("Winner \n");
        }else{
            printf("Try Again \n");
        }
    }
    if (total1==total2){
        if (guess == 's') {
            printf("Winner \n");
        }else{
            printf("Try Again \n");
        }
    }
   
return 0;
}
+1 Tyler Borntraeger · November 26, 2014
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

//Declarations
int roll1, roll2, roll3, total1 = 0, total2 = 0, i;
char guess;


//Rng roll 1
srand(time(0));
roll1 = ( rand()%6 ) + 1;
printf("First roll: %d, ", roll1);
roll2 = ( rand()%6 ) + 1;
printf("Second roll: %d, ", roll2);
roll3 = ( rand()%6 ) + 1;
printf("Third roll: %d, ", roll3);
total1 = (roll1 + roll2 + roll3);
printf("First roll total: %d \n", total1);

//Ask for guess
printf("Guess whether the next roll is bigger ('b'), smaller ('s'), or the same ('s') by entering the corresponding single letters<<\n");
scanf("%c", &guess);

//Rng roll 2
roll1 = ( rand()%6 ) + 1;
printf("First roll: %d, ", roll1);
roll2 = ( rand()%6 ) + 1;
printf("Second roll: %d, ", roll2);
roll3 = ( rand()%6 ) + 1;
printf("Third roll: %d, ", roll3);
total2 = (roll1 + roll2 + roll3);
printf("Second roll total: %d\n", total2);

//Conditionals: User guesses </>
if ( ((total1 > total2) && (guess == 's')) || ((total1 < total2) && (guess == 'b')) || ((total1 == total2) && (guess == 's')) )
printf("Waiwaiwai---How'd you know?");
else
printf("Best 2 out of 3?");

return 0;
}
0 Youknees Adoro · November 26, 2014
is there a problem with your recently posted codes?
0 Youknees Adoro · November 10, 2014
I think this can help. 


/images/forum/upload/2014-11-10/d07f4a01b64a324be4d468918d753b39.PNG
  • 1

C

107,109 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator