Skip to content

Instantly share code, notes, and snippets.

@vtu27619
Created October 25, 2025 05:23
Show Gist options
  • Save vtu27619/16d225dc612f13e8b220e1d1e34d3987 to your computer and use it in GitHub Desktop.
Save vtu27619/16d225dc612f13e8b220e1d1e34d3987 to your computer and use it in GitHub Desktop.
Likitha
import numpy as np
def simulate_coin_flips(n_flips):
"""
Simulates n_flips coin tosses.
'Heads' is represented by 1, and 'Tails' by 0.
"""
flips = np.random.randint(0, 2, size=n_flips) # 0 or 1 for each flip
heads = np.count_nonzero(flips == 1)
tails = np.count_nonzero(flips == 0)
print(f"\n--- Coin Flip Simulation ({n_flips} flips) ---")
print("Results:", ["Heads" if x == 1 else "Tails" for x in flips])
print(f"Heads: {heads}, Tails: {tails}")
print(f"Probability of Heads: {heads/n_flips:.2f}")
print(f"Probability of Tails: {tails/n_flips:.2f}")
def simulate_dice_rolls(n_rolls):
"""
Simulates n_rolls dice rolls.
Each roll produces an integer from 1 to 6.
"""
rolls = np.random.randint(1, 7, size=n_rolls)
print(f"\n--- Dice Roll Simulation ({n_rolls} rolls) ---")
print("Results:", rolls)
# Frequency count for each face
for i in range(1, 7):
count = np.count_nonzero(rolls == i)
print(f"Face {i}: {count} times ({count/n_rolls:.2f} probability)")
# Main Program
if __name__ == "__main__":
print("Random Simulation using NumPy")
# Ask user for input
n_flips = int(input("\nEnter number of coin flips: "))
simulate_coin_flips(n_flips)
n_rolls = int(input("\nEnter number of dice rolls: "))
simulate_dice_rolls(n_rolls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment