C Programming Tutorial – 38 – Random Number Generator with rand

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

26 responses to “C Programming Tutorial – 38 – Random Number Generator with rand”

  1. keshav tangri Avatar

    //My attempt for the given problem with rand.
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<math.h>
    #include<string.h>
    int main(){
    int i, n ;
    char answer;
    printf("How many times do you want to throw the dice ? ");
    scanf("%d",&n);
    int Dice_No[n+1];
    for(i=0;i<n;i++){
    Dice_No[i] = ((rand()%6)+1);
    printf("#%d roll on die : %d n",(i+1),Dice_No[i]);

    }
    printf("Will next output higher(H) or lower(L) than the previous one ? ");
    scanf(" %c",&answer);
    answer = toupper(answer);
    Dice_No[n] = (rand()%6) +1;
    printf("#%d on dice is : %d n",(n+1),Dice_No[n]);
    if((Dice_No[n]>Dice_No[n-1] && answer == 'H')|| (Dice_No[n]<Dice_No[n-1]&&answer=='L')){
    printf("USER WINS !");
    }else{
    printf("USER LOSE !");
    }
    return 0;
    }

  2. Hamam Alhar Avatar

    How do we not generate the same number which has been generated in a loop? I need this because i'm doing a quiz game.

  3. חיים זיסמן Avatar

    using only what u teached until now.
    ————————————————————–
    {
    int dice,sum1=0,sum2=0,i;
    char answer;
    for(i=0 ; i <4 ; i++)
    {
    dice = (rand()%6) + 1;
    sum1 += dice;
    }
    printf("Your sum is: %d n",sum1);
    puts("do you think, that the sum of the next drop will be higher,lower or same ? (h/l/s)");
    scanf(" %c",&answer);

    for(i=0 ; i < 3 ; i++)
    {
    dice = (rand() % 6) + 1;
    sum2 += dice;
    }
    printf("New sum is: %d nYou choose '%c'.",sum2,answer);
    if ( ((sum1 > sum2) && (answer == 'l')) || ((sum1 == sum2) && (answer == 's')) || ((sum1 < sum2) && (answer == 'h')) ){
    puts("Good For You! ");
    }
    else {
    puts("Wrong,go fuck yourself");
    }
    }
    ——————————————————–

  4. rohit patil Avatar

    i wanted a c language code for six digit random no. would you please help me.

  5. Sidhant Jain Avatar

    #include<stdio.h>
    int main()
    { int i,x,y,z,sum,sum2,j=0;
    char a;

    do{ j=0;
    srand(time(0));
    x= (rand() % 6 ) + 1;
    y= (rand() % 6 ) + 1;
    z= (rand() % 6 ) + 1;
    sum = x+y+z;
    printf("%dn",sum);
    printf("The next number will be (H/L/E):");
    scanf(" %c", &a);

    x= (rand() % 6 ) + 1;
    y= (rand() % 6 ) + 1;
    z= (rand() % 6 ) + 1;
    sum2 = x+y+z;
    printf("%dn",sum2);

    if(a == 'H' && sum < sum2)
    {printf("You win!!!n");}

    else if(a == 'L' && sum > sum2)
    {printf("You win!!!n");}

    else if(a == 'E' && sum == sum2)
    {printf("You win!!!n");}

    else{ j=1;
    printf("You loose… Try again.n");
    }
    }
    while(j == 1);

    }

  6. mohamed khaled Avatar

    Here is My Code 🙂

    char roll1;
    char roll2;
    char roll3;
    int i;
    int diceRoll1;
    int diceRoll2;
    int diceRoll3;
    int total1;
    int total2;
    char guess;
    srand (time(NULL));
    printf("tttt Welcome To Dice Roller Game ^_^nn");
    printf("Description : nt This game about 3 dice rolls and we will calculate them ,nt your next step is to guess the next dice rolls will be nt higher or lower or the same , GOOD LUCK 🙂 nn)");

    printf("Hit [r] to roll the dice :");
    scanf(" %c",&roll1);

    switch(roll1)
    {
    case 'r':

    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [1] = %dn",diceRoll1);
    }

    printf("Roll Again [r] :");
    scanf(" %c",&roll2);

    switch(roll2)
    {
    case 'r':

    for(i=0;i<1;i++)
    {
    diceRoll2 = (rand()%6) +1;
    printf(" Dice Roll [2] = %dn",diceRoll2);
    }

    printf("Roll Again [r] :");
    scanf(" %c",&roll3);

    }

    switch(roll3)
    {
    case 'r':

    for(i=0;i<1;i++)
    {
    diceRoll3 = (rand()%6) +1;
    printf(" Dice Roll [3] = %dn",diceRoll3);
    }
    break;
    }

    total1=diceRoll1+diceRoll2+diceRoll3;
    printf("Your dice rolls = %d nn",total1);
    printf("Guess next dice rolls Higher [h] ,Lower [l], Same [s] ?:");
    scanf(" %c",&guess);

    switch(guess)
    {

    case 'h':

    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [1] = %dn",diceRoll1);
    }
    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [2] = %dn",diceRoll1);
    }
    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [3] = %dn",diceRoll1);
    }
    total2 = diceRoll1+diceRoll2+diceRoll3;
    printf("Your dice rolls = %d nn",total2);

    if(total1 < total2)
    {
    printf("Guess Was Right Congratulations !!");
    }
    else{
    printf("Guess Was Wrong !!aa");

    }
    break;

    case 'l':

    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [1] = %dn",diceRoll1);
    }
    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [2] = %dn",diceRoll1);
    }
    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [3] = %dn",diceRoll1);
    }
    total2 = diceRoll1+diceRoll2+diceRoll3;
    printf("Your dice rolls = %d nn",total2);

    if(total1 > total2)
    {
    printf("Guess Was Right Congratulations !!");
    }
    else{
    printf("Guess Was Wrong !!aa");

    }
    break;

    case 's':

    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [1] = %dn",diceRoll1);
    }
    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [2] = %dn",diceRoll1);
    }
    for(i=0;i<1;i++)
    {
    diceRoll1 = (rand()%6) +1;
    printf(" Dice Roll [3] = %dn",diceRoll1);
    }
    total2 = diceRoll1+diceRoll2+diceRoll3;
    printf("Your dice rolls = %d nn",total2);

    if(total1 == total2)
    {
    printf("Guess Was Right Congratulations !!");
    }
    else{
    printf("Guess Was Wrong !!aa");

    }
    break;

    default:
    printf("Wrong Input please please make sure that your input between [h] ,[l] ,[s] Characters !aaa");
    break;

    }

    }

  7. brave anid Avatar

    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>int main()
    {    srand(time(NULL));
        int diceRoll1,game=1,diceRoll2,diceRoll3,diceRoll4,diceRoll5,diceRoll6,sum1=0,sum2=0,roll,a,repetition;
        printf("How many times do you want to play the game:");
        scanf(" %d", &repetition);
        char guess;
        for(a=1;a<=repetition;a++){
            printf("Game: %d nn",game);
            game++;        diceRoll1=rand()%6+1;
            diceRoll2=rand()%6+1;
            diceRoll3=rand()%6+1;
            printf("Dice Roll: %dnDice Roll: %dnDice Roll: %dn",diceRoll1,diceRoll2,diceRoll3);
            sum1 = diceRoll1+diceRoll2+diceRoll3;    printf("nThe sum is:%d   nWill the next sum be higher, lower or same?(h/l/s)",sum1);
        scanf(" %c", &guess);
            diceRoll4=rand()%6+1;
            diceRoll5=rand()%6+1;
            diceRoll6=rand()%6+1;
            printf("Another Dice Roll: %dnAnother Dice Roll: %dnAnother Dice Roll: %dn", diceRoll4,diceRoll5,diceRoll6);
            sum2=diceRoll4+diceRoll5+diceRoll6;    printf("nThe next sum:%dn", sum2);    if(sum2>sum1 && (guess=='H' || guess=='h')){puts("nYou higher guess was correct.nn");}
        else if(sum2==sum1 && (guess=='s' || guess=='S')){puts("nYour guess that the sum would remain same was correct.nn");}
        else if(sum2<sum1 && (guess=='l' || guess=='L')){puts("nYour lower guess was correct.nn");}
        else{puts("nYou suck");}
        }    return 0;
    }

  8. Caio Costa Avatar

    ~just a brazilian portuguese code passing by~

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<ctype.h>
    #include <time.h>

    int main()
    {
    srand(time(NULL));
    int i, dice, dice2, dice3, tdice, dice4;
    char press, press2, press3, press4, adv;

    printf("Vamos lançar dados!n");
    printf("Pressione 'j' para lancar o primeiro dado: ");
    scanf(" %c", &press);
    if(press == 'j'){
    dice = rand()%6 + 1;
    printf("Primeiro dado:n%dn", dice);
    }else if (press!='j'){
    printf("sabe teclar nao?n");
    }

    if(press == 'j'){

    printf("Pressione 'j' para lancar o segundo dado: n");
    scanf(" %c", &press2);
    if(press2 == 'j'){
    dice2 = rand()%6 + 1;
    printf("Segundo dado:n%dn", dice2);
    }else if (press2!='j'){
    printf("sabe teclar nao?n");
    }
    if (press2=='j'){

    printf("Pressione 'j' para lancar o terceiro dado: n");
    scanf(" %c", &press3);
    if(press3 == 'j'){
    dice3 = rand()%6 + 1;
    printf("Terceiro dado:n%dn", dice3);
    }else if (press3!='j'){
    printf("sabe teclar nao?n");
    }if (press3=='j'){

    tdice = dice + dice2 + dice3;
    printf("Certo, certo. A soma dos dados eh: %dn", tdice);
    printf("Tendo em vista esses resultados (que nao querem dizer nada),ncomo voce acredita que sera o proximo numero?n");
    printf("Maior, igual ou menor ao anterior?(h/s/l)n");
    scanf(" %c", &adv);

    if(adv == 'h' || adv == 'l' || adv == 's'){
    dice4=rand()%6+1;
    printf("O quarto dado vale: %dnIsso significa que voce ",dice4);
    if (adv == 'h' && dice4>dice3){
    printf("certou, mizeravi!n");
    }
    else if(adv == 's' && dice4 == dice3){
    printf("certou, mizeravi!n");
    }else if(adv=='l' && dice4<dice3)
    {
    printf("certou, mizeravi!n");
    }else{
    printf("errrrrooooou!n");
    }

    }else{
    printf("Voce eh cego?n");
    }
    }
    }
    }

    return 0;
    }

  9. yaggenlol Avatar

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    int main(void)

    {
    srand(time(NULL)); // This line makes "rand" function actually returning random values each time.
    int i,n;
    int diceRoll1,diceRoll2,diceRoll3;
    char gamePlay;
    char gamePlay2;
    int sum,sum2;

    printf("Do you wanna play a game? Y/Nn");
    scanf("%c",&gamePlay);

    if (gamePlay == 'Y' || gamePlay == 'y' ) {
    printf("Okay! Great!n");
    } else {
    printf("Boring..n");
    return 0;
    }

    for (i = 0; i <1; i++) {
    diceRoll1 = (rand()%6) +1;
    diceRoll2 = (rand()%6) +1;
    diceRoll3 = (rand()%6) +1;
    printf("Dice 1 shows: %dn", diceRoll1);
    printf("Dice 2 shows: %dn", diceRoll2);
    printf("Dice 3 shows: %dn", diceRoll3);
    }
    sum = diceRoll1 + diceRoll2 + diceRoll3;
    printf("Your first three throws has the sum of: %dn", sum);
    printf("Now we are going to throw the dices again, do you think the sum will be higher (H), lower (L) or the same (S)n:");

    scanf(" %c", &gamePlay2);

    if (gamePlay2 == 'H' || gamePlay2 == 'h' ) {
    printf("Okay! Round 2, Good luck!n");
    } else if (gamePlay2 == 'L' || gamePlay2 == 'l') {
    printf("Okay! Round 2, Good luck!n");
    } else if (gamePlay2 == 'S' || gamePlay2 == 's') {
    printf("Okay! Round 2, Good luck!n");
    }else{
    printf("Boring..n");
    return 0;
    }
    for (n = 0; n <1; n++) {
    diceRoll1 = (rand()%6) +1;
    diceRoll2 = (rand()%6) +1;
    diceRoll3 = (rand()%6) +1;
    printf("Dice 1 shows: %dn", diceRoll1);
    printf("Dice 2 shows: %dn", diceRoll2);
    printf("Dice 3 shows: %dn", diceRoll3);
    }
    sum2 = diceRoll1 + diceRoll2 + diceRoll3;
    printf("Your second three throws has the sum of: %dn", sum2);

    switch(gamePlay2) {
    case 'L': if (sum > sum2) {printf("Congratulations! You are really good at guessing!n");} else {printf("Better luck next time!n");} break;
    case 'l': if (sum > sum2) {printf("Congratulations! You are really good at guessing!n");} else {printf("Better luck next time!n");} break;
    case 'H': if (sum2 > sum) {printf("Congratulations! You are really good at guessing!n");} else {printf("Better luck next time!n");} break;
    case 'h': if (sum2 > sum) {printf("Congratulations! You are really good at guessing!n");} else {printf("Better luck next time!n");} break;
    case 's': if (sum2 == sum) {printf("Congratulations! You are really good at guessing!n");} else {printf("Better luck next time!n");} break;
    case 'S': if (sum2 == sum) {printf("Congratulations! You are really good at guessing!n");} else {printf("Better luck next time!n");} break;
    }

    }

  10. Abhinav Shukla Avatar

    Heres my program. Try it yourself 🙂

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    int main()
    {
    srand(time(NULL));
    int roll, a1, a2, u;
    char ans;
    char end;
    int numb1 = 0;
    int numb2 = 0;
    LOOP1: printf("~Welcome to the Dice Rolling Game~n");
    printf("t Intrusctions: n");
    printf("1.The Computer will role a dice for you three times. n");
    printf("2.You'll be asked if the second time around, n when the computer roles a dice if the number be higher, lower, or if its the the same. n");
    printf("3.If the number is the same: You Win!~ n");
    printf("t Press '0' to continue: n");
    scanf(" %d" ,&u);
    for(u=0; u<3; u++) {
    roll = rand() %6 + 1 ;
    printf("%d n" ,roll);
    numb1 += roll;
    }
    printf("n The final sum is %d. " , numb1);
    ans = numb1;
    LOOP: printf("Do you think that the second time around, the sum will be higher, lower, or the same? n");
    printf("If you think it'll be the same, type:'s', For higher: 'h' and for lower 'l':");
    scanf(" %c" ,&ans);
    if (ans == 's' || ans == 'h' || ans == 'l'){
    printf("Alright! Here goes for the second time around!" );
    }else{
    printf("Thats Not valid! Please Type 's' 'h' or 'l'");
    goto LOOP;
    }
    for(u=0; u<3; u++) {
    roll = rand() %6 + 1 ;
    printf(" n %d n" ,roll);
    numb2 += roll;
    }
    printf("The total the sum the second time around was %d n", numb2);

    switch(ans){
    case 'l' : if(numb2 < numb1) printf("You were right! The number was lower!");
    else printf("ERRR! YOU WERE INCORRECT!"); break;
    case 'h' : if(numb2 > numb1) printf("You were right! The number was higher!");
    else printf("ERRR! YOU WERE INCORRECT!"); break;
    case 's' : if(numb2 == numb1) printf("You were right! The number was higher!");
    else printf("ERRR! YOU WERE INCORRECT!"); break;
    }

    printf(" n Please Press 'Y' if you want to continue or press 'N' if you want to end the program. ");
    scanf(" %c" ,&end);
    if(end == 'Y' || end == 'y') {
    goto LOOP1;
    }else if(end == 'N' || end == 'n'){
    printf(" n Fine then quitter.");
    return 0;
    }
    else {
    printf("n Thats not a valid option! Please input Y, or N, not anything else!!");
    }

    return 0;
    }

  11. Flavio Gaming Avatar

    I'm addicted to this game now! lol. Here's the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    int main()
    {
    int dice1, dice2, dice3, dice4, dice5, dice6, total1, total2, score=0;
    char nextroll;
    start:
    srand(time(NULL));
    dice1=rand()%6+1;
    dice2=rand()%6+1;
    dice3=rand()%6+1;
    total1=dice1+dice2+dice3;
    printf("Dice 1: %dnDice 2: %dnDice 3: %dnTotal : %dn", dice1, dice2, dice3, total1);
    printf("Will the next roll be higher(h), lower(l) or the same(s)?");
    scanf(" %c", &nextroll);
    dice4=rand()%6+1;
    dice5=rand()%6+1;
    dice6=rand()%6+1;
    total2=dice4+dice5+dice6;
    printf("Dice 1: %dnDice 2: %dnDice 3: %dnTotal : %dn", dice4, dice5, dice6, total2);
    if(nextroll=='h'&&total2>total1 || nextroll=='s'&&total2==total1 || nextroll=='l'&&total2<total1){
    printf("Good Job! Your prediction was right!n");
    score=score+1;
    }
    else{printf("Your prediction sucks!n");
    score=score-1;
    }
    printf("Your score is %dn", score);
    goto start;
    }

  12. Meak Studios Avatar

    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    #include<math.h>
    #include<time.h>
    int main()
    {
    srand(time(NULL));
    int diceRollSum=0 , i , initialDiceSum;
    char letter;

    for(i=1; i<=3; i++){

    diceRollSum += (rand()%6 + 1);

    }
    initialDiceSum = diceRollSum;

    printf("The dice has been rolled 3 times nThe sum is : %d n Tell Me the if the next dice roll would be higher , lower or will be the same ? (h,l,s) : ",diceRollSum);
    scanf("%c",&letter);
    diceRollSum = diceRollSum + ((rand()%6) + 1);
    if(letter == 'h'){
    if(initialDiceSum > diceRollSum){
    printf("you Are correct");
    }else{
    printf("you sucks!");
    }
    }else if(letter == 'l'){
    if(initialDiceSum < diceRollSum){
    printf("you Are correct");
    }else{
    printf("you sucks!");
    }
    }else if(letter == 's'){
    if(initialDiceSum == diceRollSum){
    printf("you Are correct");
    }else{
    printf("you sucks!");
    }
    }

    return 0;
    }

    This will not give the same random numer each time !

  13. eli Avatar

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    #include <time.h>
    int main()
    {
    int a,b,c,d,e,f,g,h,i;
    printf("Please enter 1 to roll first dice: ");
    scanf("%d",&a);
    printf("Please enter 2 to roll second dice: ");
    scanf("%d",&b);
    printf("Please enter 3 to roll third dice: ");
    scanf("%d",&c);
    srand(time(NULL));
    if (a==1 && b==2 && c==3){
    h=((rand()%6)+1)+((rand()%6)+1)+((rand()%6)+1);
    printf("Your total sum is %dn",h);
    printf("Guess what will the next roll give: n");
    printf("Enter 1 if you think it will be greater.n");
    printf("Enter 2 if you think it will be smaller.n");
    printf("Enter 3 if you think it will be same.n");
    scanf("%d",&f);
    i=((rand()%6)+1)+((rand()%6)+1)+((rand()%6)+1);
    printf("Your another sum was %dn",i);
    if (f==1 && i>h){
    printf("You were right");
    } else if (f==2 && i<h){
    printf("You were right");
    }else if (f==3 && i==h){
    printf("You were right");
    }else {
    printf("You were wrong!");
    }
    }
    return 0;
    }

  14. Sandeep Ghemire Avatar

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<time.h>
    int main()
    {
    srand(time(NULL));
    printf("Welcome to the Roll Gamen");
    int dice1,dice2,dice3,dice4;
    printf("Enter any key to roll the First dicen");
    int A=getchar();
    int a;
    while((a=getchar())!=EOF && a!='n')
    ;
    dice1=(rand()%6)+1;
    printf("Its %dn",dice1);
    printf("Enter any key to roll the Second dicen");
    int B=getchar();
    int b;
    while((b=getchar())!=EOF && b!='n')
    ;
    dice2=(rand()%6)+1;
    printf("Its %dn",dice2);
    printf("Enter any key to roll the Third dicen");
    int C=getchar();
    int c;
    while((c=getchar())!=EOF && c!='n')
    ;
    dice3=(rand()%6)+1;
    printf("Its %dn",dice3);
    int sum=(dice1+dice2+dice3);
    printf("The sum of all the Rolls is %dn",sum);
    printf("Now,Guess if the last Number generated from system gonna be:n1.Lower than the sumn2.Higher than the sumn3.Same as sumn");
    printf("Press 1 for LowernPress 2 for HighernPress 3 for Samen");
    int D=getchar();
    int d;
    while((d=getchar())!=EOF && d!='n')
    ;
    dice4=(rand()%30)+1;
    printf("The system Generated Number is %dn",dice4);
    if(D=='1')//or D==49
    {
    if(dice4<sum)
    {
    printf("Good Job,You wonn");
    }
    else
    printf("Sorry You Losen");
    }
    if(D==50)
    {
    if(dice4>sum)
    {
    printf("Good Job,You wonn");
    }
    else
    printf("Sorry You Losen");
    }
    if(D==51)
    {
    if(dice4==sum)
    {
    printf("Good Job,You wonn");
    }
    else
    printf("Sorry You Losen");
    }
    return 0;
    }

  15. linh nguyen Avatar

    This is my code and it works.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    int i,a;
    int diceroll1,diceroll2,diceroll3;
    int total1;
    srand ( time(NULL));

    printf("First, you will roll 3 dices at the same time, and it return total of their value. Enter any word to rolln");
    scanf(" %c", &a);

    for (i=0; i<1;i++){
    diceroll1=(rand()%6)+1;
    diceroll2=(rand()%6)+1;
    diceroll3=(rand()%6)+1;
    }

    printf("the first roll: %dn", total1=diceroll1+diceroll2+diceroll3);

    char result;
    int total2;

    printf("guess if the next roll is going to be higher(press H), lower(press L), or the same(press S)n");
    scanf(" %c", &result);

    for (i=0; i<1;i++){
    diceroll1=(rand()%6)+1;
    diceroll2=(rand()%6)+1;
    diceroll3=(rand()%6)+1;
    }

    printf("the second roll: %dn", total2=diceroll1+diceroll2+diceroll3);

    switch (result){
    case 'H' : if (total1<total2){
    printf("GOOD JOBn");
    }else{
    printf("YOU SUCKn");
    }
    break;

    case 'L' : if (total1>total2){
    printf("GOOD JOBn");
    }else{
    printf("YOU SUCKn");
    }
    break;

    case 'S' : if (total1=total2){
    printf("GOOD JOBn");
    }else{
    printf("YOU SUCKn");
    }
    break;

    default : printf("It doesn't make sense");

    }

    }

  16. cy world Avatar

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>

    int main()
    {

    int i;
    int FirstDiceRoll=0;
    int SecondDiceRoll = 0;
    int DiceRoll = 0;
    char RollCount[4] = "abc";
    char TheAnswer;
    char CorrectAnswer;
    srand (time(NULL));

    for (i=0; i<3; i++)
    {
    DiceRoll = ( rand()%6) + 1; //generates ramdom divided by 6 with remainder
    printf("Dice %c is: %d n", RollCount[i], DiceRoll); //print output remainder of random divided by 6
    FirstDiceRoll = FirstDiceRoll + DiceRoll;
    }
    DiceRoll = 0;
    printf("n3 rolls of dice for a total of: %dn", FirstDiceRoll);
    printf("nDo you think the next three rolls total will ben");
    printf("nHigher enter H, Lower enter L, or Equal enter S than %d?n", FirstDiceRoll);

    scanf(" %c", &TheAnswer);

    printf("you entered %cnn", (TheAnswer));

    for (i=0; i<3; i++)
    {
    DiceRoll = ( rand()%6) + 1; //generates ramdom divided by 6 with remainder
    printf("Dice %c is: %d n", RollCount[i], DiceRoll); //print output remainder of random divided by 6
    SecondDiceRoll = SecondDiceRoll + DiceRoll;
    }

    if(SecondDiceRoll <= FirstDiceRoll)
    {
    CorrectAnswer = 'L';
    }
    else if (SecondDiceRoll >= FirstDiceRoll)
    {
    CorrectAnswer = 'H';
    }
    else
    {
    CorrectAnswer = 'S';
    }
    printf("nSecond 3 rolls of dice for a total of: %dnn", SecondDiceRoll);

    printf("The Correct answer answer is: %c, your answer was %cnn", CorrectAnswer, TheAnswer);

    printf("1st roll total: %d, 2nd roll total: %dnn", FirstDiceRoll, SecondDiceRoll);

    if (CorrectAnswer == TheAnswer)
    {
    printf("You are rightn");
    }
    else
    {
    printf("You are wrongn");
    }

    }

  17. ying tang Avatar

    After I read other codes, I realized that I misinterpreted the problem requirement.
    I compared the number of the fourth dice roll withe the sum number of the prior three rolls.
    Anyway, lazy to change, the principle is same. 🙂

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <math.h>
    #include <string.h>

    int main()
    {

    int dice1,dice2,dice3,dicesum,dice4;
    char guess,apple;
    dice1=rand()%6+1;
    dice2=rand()%6+1;
    dice3=rand()%6+1;
    dicesum=dice1+dice2+dice3;
    dice4=rand()%6+1;

    printf("dice1:%dt dice2:%dt dice3:%dn",dice1,dice2,dice3);
    printf("dicesum:%dn",dicesum);

    printf("The number of the fourth dice roll would be higher, lower or same as the sum of the prior three rolls?(h,l or s)n");
    scanf(" %c", &guess);

    printf("dice4:%dn",dice4);

    if(dice4-dicesum>0){
    apple='h';
    }else{
    if(dice4-dicesum==0){
    apple='s';
    }else{
    apple='l';
    }
    }

    if(guess==apple){
    printf("You are right!n");
    }else{
    printf("Poor guess.");
    }

    return 0;
    }

  18. Ritvik Joshi Avatar

    I did it easily guys. Check it out

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>int main()
    {
        int i;
        int diceroll1;
        int diceroll2;
        int diceroll3;
        int dicerollsum;
        int previousroll=0;
        srand(time(NULL));
        printf(" %d n",0);
        for(i=0; i<20; i++){
            char guess;
            printf("Guess what the next number would be(h/l/e): ");
            scanf(" %c",&guess);
            diceroll1=( rand()%6 ) + 1;
            diceroll2=( rand()%6 ) + 1;
            diceroll3=( rand()%6 ) + 1;
            dicerollsum = diceroll1 + diceroll2 + diceroll3;
            printf(" %d n",dicerollsum);
            if((guess=='h' && dicerollsum>previousroll) ||(guess=='l' && dicerollsum<previousroll) || (guess=='e' && dicerollsum==previousroll)){
                printf("WIN! n");
            }else{
            printf("LOSE! n");
            }previousroll=dicerollsum;
        }
        return(0);
    }

  19. Jaskirat Singh Avatar

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    int sum();
    int main()
    {
    int a,b,dice1,dice2,dice3;
    char c;
    puts("welcome to dice guess game , npress enter when you ready to play");
    getchar();

    dice1=(rand() %6) +1;
    dice2=(rand() %6) +1;
    dice3=(rand() %6) +1;
    a=dice1 +dice2 +dice3;

    puts("sum equals ");
    printf("%d",a);
    puts("n guess the next no.n h for higher n l for lower n s for same");
    scanf("%c",&c);

    b=((rand() %6) +1 +(rand() %6) +1 +(rand() %6) +1);

    if(c=='h' && b>a || c=='l' && b<a || c=='s' && b==a)
    {
    puts("well done");
    }
    else
    {
    puts("you sucks");
    }
    }

  20. Ritvik Joshi Avatar

    My cmd stops working on rand command. Help

  21. Alex Lotus Avatar

    But how do you generate numbers between some other starting point then 0? For example 75 to 105?
     PLEASE GUYS HELP!

  22. Caput Avatar

    Thanks for the tutorials 😉

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>

    int main(){

    char roll;
    int dice1=0, dice2=0, dice3=0;
    int result=0, result1=0;
    char decision;

    do{

    printf("Press 'r' to roll or 'q' to quit.n");
    scanf(" %c", &roll);

    if(roll== 'r'){

    dice1 = (rand()%6)+1;

    dice2 = (rand()%6)+1;

    dice3 = (rand()%6)+1;

    result= dice1 + dice2 + dice3;

    printf("Dice1= %d Dice2= %d Dice3= %dnSum= %dn", dice1, dice2, dice3, result);
    }else{
    if(roll == 'q'){
    break;
    }
    }

    printf("Will it be higher (h) the same (s) or lower(l)?n");
    scanf(" %c", &decision);

    switch(decision){
    case 'h':
    dice1 = (rand()%6)+1;

    dice2 = (rand()%6)+1;

    dice3 = (rand()%6)+1;

    result= dice1 + dice2 + dice3;

    printf("Dice1= %d Dice2= %d Dice3= %dnSum= %dn", dice1, dice2, dice3, result);

    if (result<result1){
    printf("You win!n");
    }else{
    printf("You lose.n");
    }
    break;
    case 's':
    dice1 = (rand()%6)+1;

    dice2 = (rand()%6)+1;

    dice3 = (rand()%6)+1;

    result= dice1 + dice2 + dice3;

    printf("Dice1= %d Dice2= %d Dice3= %dnSum= %dn", dice1, dice2, dice3, result);

    if (result == result1){
    printf("You win!n");
    }else{
    printf("You lose.n");
    }
    break;
    case 'l':
    dice1 = (rand()%6)+1;

    dice2 = (rand()%6)+1;

    dice3 = (rand()%6)+1;

    result= dice1 + dice2 + dice3;

    printf("Dice1= %d Dice2= %d Dice3= %dnSum= %dn", dice1, dice2, dice3, result);

    if (result>result1){
    printf("You win!n");
    }else{
    printf("You lose.n");
    }
    break;

    }

    }while(roll!='q');

    return 0;

    }

  23. Axe Box Avatar

    What if we want to create more than one ranges like, we want it to give number from these three ranges
    101-110
    201-210
    301-310?

  24. TheMysteriousHi Avatar

    Here's my code for the challenge:

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>

    int main()
    {
    char roll[3];
    int diceRoll;
    int i = 1;
    int diceRollSum = 0; // diceRollSum += diceRoll;
    srand(time(NULL));

    printf("Type roll or r to roll your dice! n");
    scanf(" %s", roll);

    if(roll[0] == 'r' || roll[0] == 'R'){
    while(i<=3){

    switch(i){
    case 1 : i++;
    diceRoll = (rand()%20) + 1;
    diceRollSum += diceRoll;
    printf("Your first roll is %d, roll again! n", diceRoll);
    scanf(" %s", roll);

    case 2 : i++;
    diceRoll = (rand()%20) + 1;
    diceRollSum += diceRoll;
    printf("Your second roll is %d, roll one more time! n", diceRoll);
    scanf(" %s", roll);

    case 3 : i++;
    diceRoll = (rand()%20) + 1;
    diceRollSum += diceRoll;
    printf("Your last roll is %d, and the sum of your rolls is %d. nType m, l, or s if you think your next roll will be more, less, or the same as the sum. n", diceRoll, diceRollSum);
    scanf(" %s", roll);
    }
    }

    diceRoll = (rand()%20) + 1;
    printf("Your next roll was %d… ", diceRoll);

    if(diceRoll>diceRollSum && (roll[0] == 'm' || roll[0] == 'M') )
    printf("You win!");
    else if(diceRoll<diceRollSum && (roll[0] == 'l' || roll[0] == 'L') )
    printf("You win!");
    else if(diceRoll==diceRollSum && (roll[0] == 's' || roll[0] == 'S') )
    printf("You win!");
    else printf("You lose :'(");

    }

    return 0;
    }

  25. Abbod Zakout Avatar

    Here's my code for the challenge :

    {
    int i;
    int diceRoll1;
    int diceRoll2;
    int diceRoll3;
    int total;

    printf("Your First Dice Roll! n");
    for(i=0; i<1; i++) {
    diceRoll1 = (rand() %6) + 1;
    printf("%d n", diceRoll1);
    }

    printf("n n Your Second Dice Roll! n");
    for(i=0; i<1; i++) {
    diceRoll2 = (rand() %6) + 1;
    printf("%d n", diceRoll2);
    }

    printf("n n Your Third Dice Roll! n");
    for(i=0; i<1; i++) {
    diceRoll3 = (rand() %6) + 1;
    printf("%d n", diceRoll3);
    }

    total = diceRoll1+diceRoll2+diceRoll3;

    printf("n n Total of your rolls is: %d n", total);

    return 0;
    }

  26. Diego Martins Avatar

    For those who are still wondering about a solution to challenge #2:

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>

    int main()
    {
    int dice1;
    int dice2;
    int dice3;
    int dice4;
    int dice5;
    int dice6;
    int diceTotal1;
    int diceTotal2;
    char userAnswer;
    char userAnswer2;

    printf("tttaWelcome to the casino!!nn");

    do{

    printf("nPress any key to roll the dice!nn");
    getchar();

    srand(time(NULL));

    dice1 = (rand()%6) + 1;
    dice2 = (rand()%6) + 1;
    dice3 = (rand()%6) + 1;
    diceTotal1 = dice1 + dice2 + dice3;

    printf("Dice1: %d nDice2: %d nDice3: %d nnTotal: %dnn", dice1, dice2, dice3, diceTotal1);
    printf("If you guess your next dice roll is gonna be higher press (h).nIf you think it's gonna be lower press (l).nIf you think it's gonna be the equal press (e).n");
    scanf("%c", &userAnswer);

    srand(time(NULL));

    dice4 = (rand()%6) + 1;
    dice5 = (rand()%6) + 1;
    dice6 = (rand()%6) + 1;
    diceTotal2 = dice4 + dice5 + dice6;

    printf("nDice1: %d nDice2: %d nDice3: %d nnTotal: %dnn", dice4, dice5, dice6, diceTotal2);

    if((userAnswer == 'h' && diceTotal1 < diceTotal2) || (userAnswer == 'l' && diceTotal1 > diceTotal2) || (userAnswer == 'e' && diceTotal1 == diceTotal2))
    {
    printf("Very good. You guess right! nIf you want to try again press (1). If you want to leave press (0)n");
    scanf(" %c", &userAnswer2);
    }
    else
    {
    printf("You guessed wrong. Try again! nIf you want to try again press (1). If you want to leave press (0)n");
    scanf(" %c", &userAnswer2);
    }

    }while(userAnswer2 != '0');

    return 0;
    }

    This solution allows the user to "play" the guessing game endlessly.

Leave a Reply to eli Cancel reply

Your email address will not be published. Required fields are marked *