Advertisement
MissDizzy1

Untitled

Mar 4th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import random
  2.  
  3. def flip_coin():
  4.     result = random.choice(["Heads", "Tails"])
  5.     print(result)
  6.  
  7. flip_coin()
  8.  
  9. Code Output
  10.  
  11. Heads
  12.  
  13. Use code with caution.
  14.  
  15. JavaScript
  16. JavaScript
  17.  
  18. function flipCoin() {
  19.   const sides = ["Heads", "Tails"];
  20.   const result = sides[Math.floor(Math.random() * 2)];
  21.   console.log(result);
  22. }
  23.  
  24. flipCoin();
  25.  
  26. Use code with caution.
  27.  
  28. Bash (Linux/macOS)
  29. Bash
  30.  
  31. flip_coin() {
  32.   if [ $((RANDOM % 2)) -eq 0 ]; then
  33.     echo "Heads"
  34.   else
  35.     echo "Tails"
  36.   fi
  37. }
  38.  
  39. flip_coin
  40.  
  41. Use code with caution.
  42.  
  43. Explanation:
  44.  
  45.     Import Random Module: In Python and JavaScript, we import the random (or Math in JavaScript) module to generate random numbers.
  46.     Coin Sides: We define the possible outcomes: "Heads" and "Tails".
  47.     Random Choice: We use functions like random.choice (Python) or Math.random (JavaScript) to randomly pick one of the sides.
  48.     Bash Version: The Bash version uses the RANDOM variable for randomness and a conditional to determine "Heads" or "Tails".
  49.  
Tags: coins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement