Guess Game using rand() and srand() in C Last Updated : 29 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Using srand() and rand() function in C, a simple but interesting game can be made. This game is called "Guess Game" . Rules of the Game : There are three holes . A rat is hidden in one of those three holes.The Rat shuffles its position every time.You have to guess the hole in which the Rat is hidden among the three holes.The hole in which Rat is present is named as 'R' and rest two are named as 'N'.You have some cash(inhand_cash) with you.You make a bet (amount_bet) for playing this game every time you make a guess.If your guess is wrong, you lose the amount_bet from your inhand_cash.If you guess is right, you win twice the amount_bet in your inhand_cash.Keep playing and keep winning until you go out of cash. Below is the code in C for this simple and interesting game: Note : As this game takes input from the players for their inhand_cash, bet_amount and the guessed location of the rat, so this will not run in online compiler. C // Cpp program for guessing game // using rand() and srand() #include <stdio.h> #include <stdlib.h> #include <time.h> void GuessGame(int amount_bet, int* inhand_cash) { char Hole[3] = { 'N', 'R', 'N' }; printf("\nWait !! Rat is shuffling its position...\n"); srand((time(NULL))); int i, x, y, temp; /*Swapping the Rat's (R's) position five times using the random number for random index*/ for (i = 0; i < 5; i++) { x = rand() % 3; y = rand() % 3; temp = Hole[x]; Hole[x] = Hole[y]; Hole[y] = temp; } int PlayerGuess; printf("\nYou may now guess the hole in which Rat is present: "); scanf("%d", &PlayerGuess); if (Hole[PlayerGuess - 1] == 'R') { (*inhand_cash) += 2 * amount_bet; printf("You win ! The holes are as follows: "); printf("\"%c %c %c\" ", Hole[0], Hole[1], Hole[2]); printf("\nYour inhand_cash is now = %d \n", *inhand_cash); } else { (*inhand_cash) -= amount_bet; printf("You Lose ! The holes are as follows: "); printf("\"%c %c %c\" ", Hole[0], Hole[1], Hole[2]); printf("\nYour inhand_cash is now = %d \n", *inhand_cash); } } int main() { int amount_bet, inhand_cash; /* You have to guess the hole in which the Rat is hidden among three holes The hole in which Rat is present is named as 'R' and rest two are named as 'N' If your guess is wrong, you lose the amount_bet from your inhand_cash If you guess it right, you win twice the amount_bet in your inhand_cash Keep playing and keep winning until you go out of cash */ printf("----Enter the inhand_cash you have right now---- : "); scanf("%d", &inhand_cash); while (inhand_cash > 0) { printf("\nEnter the amount_bet you want to play for : "); scanf("%d", &amount_bet); if (inhand_cash == 0 || amount_bet > inhand_cash) break; GuessGame(amount_bet, &inhand_cash); } if (inhand_cash == 0 || amount_bet > inhand_cash) { printf("\n\"" " :-( Sorry you don't have enough cash to play more, "); printf("Do come next time\"" "\n"); printf("Thank You for playing :-) \n"); } return 0; } Note: This output is not taken from online compiler Output: ----Enter the inhand_cash you have right now---- : 1 Enter the amount_bet you want to play for : 1 Wait !! Rat is shuffling its position... You may now guess the hole in which Rat is present: 1 You Lose ! The holes are as follows: "N N R" Your inhand_cash is now = 0 " :-( Sorry you don't have enough cash to play more, Do come next time" Thank You for playing :-) Comment More infoAdvertise with us Next Article Output of C programs | Set 33 (rand() and srand()) C codestorywithmik Follow Improve Article Tags : Technical Scripter C Language C-Library Similar Reads Number guessing game in Python 3 and C The objective of this project is to build a simple number guessing game that challenges the user to identify a randomly selected number within a specified range. The game begins by allowing the user to define a range by entering a lower and an upper bound (for example, from A to B). Once the range i 4 min read Output of C programs | Set 33 (rand() and srand()) You came across how to generate random numbers and use of two C function rand() and srand() from the article rand() and srand() in C\C++.Listed below are some output related multiple choice question on random numbers.1. Return type of rand() function is: a) short b) int c) char d) float Answer: b Ex 4 min read Random Number Memory Game in C In this article, will design a simple number memory game in the C programming language. It is a simple memory number game where a random number is displayed and is hidden after some time. The task is to guess that displayed number to continue the game. How to Play This Game: Press 1 on your keyboard 2 min read Generate a Random Float Number in C++ Random floating numbers can be generated using 2 methods: Using rand()Using uniform real distribution1. Use of rand() We can generate random integers with the help of the rand() and srand() functions. There are some limitations to using srand() and rand(). To know more about the srand() and rand() f 5 min read Generating Test Cases (generate() and generate_n() in C++) Generating test cases for array programs can be a cumbersome process. But the generate and generate_n functions in the STL (Standard Template Library), come handy to populate the array with random values. generate() The generate functions assigns random values provided by calling the generator funct 2 min read Create Bingo Game Using C In this article, we will learn the process of creating a Bingo Game using the C programming language. What is a Bingo Game?Bingo is a social game where players are given cards with grids of numbers. The goal is to mark off a specific pattern of numbers on the card, usually five in a row, column, or 12 min read Like