Projects Inventory

Simulate Coin Toss Gaming Project in C++

Explanation

  1. Headers:
    • <iostream>: For input and output operations.
    • <cstdlib>: For rand() and srand()
      Advertisement
      functions.
    • <ctime>: For time() function to seed the random number generator.
  2. Main Function:
    • Initialize Random Seed:
      • srand(static_cast<unsigned>(time(0)));: Seeds the random number generator with the current time. This ensures different random sequences each time the program is run.
      • Advertisement
    • Simulate Coin Toss:
      • int toss = rand() % 2;: Generates a random number, either 0 or 1. The % 2 operation limits the range to 0 and 1, simulating two possible outcomes for the coin toss.
    • Output Result:
      • Checks the value of toss. If it is 0, it outputs “Heads”; if it is 1, it outputs “Tails”.

Notes:

Exit mobile version