# [source code] C Programming Tutorial - 38 - Random Number Generator with rand

 Bucky Roberts · August 7, 2014 #include #include #include #include #include int main(){    int i;    int diceRoll;    for(i=0; i<20; i++){        //returns a random integer from 0 to 32767        printf("%d \n", rand() );        //returns a number 1-6        diceRoll = ( rand()%6 ) + 1;        printf("You rolled a %d \n", diceRoll );    }    return 0;}**** CHALLENGE: Create a game where  ****User rolls three dicePrints out totalUser guess if next roll will be (h)igher, (l)ower, (s)ameRoll dice again and get totalGuess right? print out “lucky guess”Guess wrong? print out “you suck!”

## Replies

 Abishek Dhanpal · November 8, 2015 #include #include #include #include #include int main(){    int i=0;    int dice1,dice2,dice3,dice4,dice5,dice6;    char player1[20];    char player2[20];    int total1=0;    int total2=0;    srand(time(NULL));    printf("welcome to dice_game\n");    printf("enter player1 name:");    scanf("%s",player1);    printf("enter player2 name:");    scanf("%s",player2);    for(i=0;i<1;i++){        dice1=(rand()%6)+1;        printf("%d\n",dice1);    }     for(i=0;i<1;i++){        dice2=(rand()%6)+1;        printf("%d\n",dice2);    }     for(i=0;i<1;i++){        dice3=(rand()%6)+1;        printf("%d\n",dice3);    }     total1=dice1+dice2+dice3;     printf("total1 is: %d\n",total1);srand(time(NULL));  for(i=0;i<1;i++){        dice4=(rand()%6)+1;        printf("%d\n",dice4);    }     for(i=0;i<1;i++){        dice5=(rand()%6)+1;        printf("%d\n",dice5);    }     for(i=0;i<1;i++){        dice6=(rand()%6)+1;        printf("%d\n",dice6);    }     total2=dice4+dice5+dice6;     printf("total2 is: %d\n",total2);     if(total1>total2){        printf("%s has won",player1);     }       if(total1#include #include #include #include int main(){int i;int value;for (i=0; i<20; i++){//print out 20 different random numbers    value = (rand()%2);//value is either 0 or 1    printf("%d \n", value);}return 0;} Omar Rios · October 19, 2015 [#include #include #include #include #include #include /* Player will roll a dice 4 times. The player has to guess the 4th time if the dice is: Higher Lower Same Note: This program was developed because I don't listen to instructions.*/int main(){ int x = 1; //counter variable int Dice1; //1st roll int Dice2; //2nd roll int Dice3; //3rd roll int Dice4; //4th roll char NextRoll[1]; //Player's choice char Roll[1]; //roll dice// FOR loop to roll the dice the first 3 times for ( x; x <= 3; x++){ srand(time(NULL)); if (x == 1){ printf(" Type any letter to roll the dice "); scanf(" %c", Roll); Dice1 = ( rand()%6 ) + 1; printf("First roll: %d \n", Dice1); }else if ( x == 2){ printf(" Type any letter to roll the dice "); scanf(" %c", Roll); Dice2 = ( rand()%6 ) + 1; printf("Second roll: %d \n", Dice2); }else if ( x == 3){ printf("Type any letter to roll the dice "); scanf(" %c", Roll); Dice3 = ( rand()%6 ) + 1; printf("Third roll: %d \n", Dice3); } } printf(" Is the next roll higher, lower or same? Type 'H', 'L', or 'S' "); scanf(" %s", NextRoll); Dice4 = ( rand()%6 ) + 1; printf("Your pick: %c \n", NextRoll[0]); printf("Forth roll: %d \n", Dice4); // test the first 3 rolls against the forth if ( (( NextRoll[0] == 'H' ) && ( Dice4 > Dice3 )) || (( NextRoll[0] == 'L' ) && ( Dice4 < Dice3 )) || (( NextRoll[0] == 'S' ) && ( Dice4 == Dice3 ))){ printf("You win!"); }else{ printf("You loose, better luck next time!"); } return 0;}][/code]
• 1

## C

118,298 followers