Skip to content

Commit 3e3e77c

Browse files
committed
Added random_module to Python-Basic-to-Advance repo
1 parent 4fa4398 commit 3e3e77c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

random_module.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random # Import Python's random module
2+
3+
# 🎲 Dice roller → generates a random integer between 1 and 6
4+
dice = random.randint(1, 6)
5+
print("🎲 Dice rolled:", dice)
6+
7+
# 🔢 Random float → generates a random value between 0.0 and 1.0
8+
print(random.random())
9+
10+
# 🎯 Random integer → generates a random integer between 0 and 9
11+
print(random.randrange(10))
12+
13+
# 🎯 Random integer with step → generates a random integer from 0, 5, 10, or 15
14+
print(random.randrange(0, 20, 5))
15+
16+
# 🎯 Random integer → generates a random integer between 10 and 19
17+
print(random.randrange(10, 20))
18+
19+
# 🌊 Random float → generates a random float between 10 and 20
20+
print(random.uniform(10, 20))
21+
22+
# 🎨 Random choice → selects a random element from the list
23+
colors = ["red", "blue", "green", "yellow"]
24+
print(random.choice(colors)) # Example output: "green"
25+
26+
# 🃏 Shuffle → randomly reorders the elements in the list
27+
cards = ["♠️ Ace", "♥️ King", "♦️ Queen", "♣️ Jack"]
28+
random.shuffle(cards)
29+
print("After shuffle:", cards)

0 commit comments

Comments
 (0)