C Programming Tutorial - Random Number Generator with rand


1. 
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<ctype.h>

#include<string.h>



int main() {
 int i;
 int diceRoll;
  
 for( i = 0; i<20; i++){
    printf("%d \n", rand());
 }



   return 0;

}


2.
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<ctype.h>

#include<string.h>



int main() {
 int i;
 int diceRoll;
  
 for( i = 0; i<20; i++){
    diceRoll = (rand()%6) +1; (divide the number by 6 and gives the remainder and add 1 to it)
    printf("%d \n", diceRoll);
 }



   return 0;

}

3.
I want to give you a challenge where I want you to create a game where

the user rolls three dice and it prints out their total to them on dice a plus b plus dice c so  after it gets the sum the user is going to look at it and it's going to guess if the next roll is to be higher and if it is they'll just type in H and press Enter lower in the press L or the same they'll press s and hit enter and then they're going to roll the dice again and if they got it right if they guess correctly we'll just like print out good job if they got it wrong print out you suck

that's it
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <math.h> int main() { int looper; int diceCounter = 1; int result; int result2; int sum = 0; int sum2 = 0; char response; srand (time(NULL)); // to make the randomization independent of the computer's fixed seed for (looper=1; looper<=3; looper++) { printf("Dice #%d:\t%d\n", diceCounter, result=rand()%6+1); sum += result; diceCounter++; } printf("Three dices were rolled.\nTheir sum is \t%d\n", sum); printf("Guess if the next three dice rolls' sum will be higher than, lower than or equal to that sum: (type L, H or E)"); scanf("%c", &response); response = toupper(response); for (looper=1; looper<=3; looper++) { printf("Dice #%d:\t%d\n", diceCounter, result2=rand()%6+1); sum2 += result2; diceCounter++; } printf("Three dices were rolled.\nTheir sum is \t%d\n", sum2); switch (response) { case 'L' : if (sum > sum2) {printf("You were right!!!");} if (sum == sum2) {printf("You were dead wrong :(");} if (sum < sum2) {printf("You were dead wrong :(");} break; case 'H' : if (sum > sum2) {printf("You were dead wrong :(");} if (sum == sum2) {printf("You were dead wrong :(");} if (sum < sum2) {printf("You were right!!!");} break; case 'E' : if (sum > sum2) {printf("You were dead wrong :(");} if (sum == sum2) {printf("You were right!!!");} if (sum < sum2) {printf("You were dead wrong :(");} break; default : printf("You typed wrong"); } return 0; }

Comments