Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def flip_coin():
- result = random.choice(["Heads", "Tails"])
- print(result)
- flip_coin()
- Code Output
- Heads
- Use code with caution.
- JavaScript
- JavaScript
- function flipCoin() {
- const sides = ["Heads", "Tails"];
- const result = sides[Math.floor(Math.random() * 2)];
- console.log(result);
- }
- flipCoin();
- Use code with caution.
- Bash (Linux/macOS)
- Bash
- flip_coin() {
- if [ $((RANDOM % 2)) -eq 0 ]; then
- echo "Heads"
- else
- echo "Tails"
- fi
- }
- flip_coin
- Use code with caution.
- Explanation:
- Import Random Module: In Python and JavaScript, we import the random (or Math in JavaScript) module to generate random numbers.
- Coin Sides: We define the possible outcomes: "Heads" and "Tails".
- Random Choice: We use functions like random.choice (Python) or Math.random (JavaScript) to randomly pick one of the sides.
- Bash Version: The Bash version uses the RANDOM variable for randomness and a conditional to determine "Heads" or "Tails".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement