General Programming & Random Module
General Programming & Random Module
Key Differences:
Type
At compile-time At runtime
Checking
Error
Early (during compilation) During execution
Detection
import random
1. random.random():
Copy
Download
2. random.randint(a, b):
Copy
Download
dice_roll = random.randint(1, 6) # 1, 2, 3, 4, 5, or 6
3. random.uniform(a, b):
Copy
Download
temp = random.uniform(-5.0, 10.0) # e.g., 3.7, -2.4
4. random.choice(sequence):
5. random.sample(population, k):
6. random.shuffle(sequence):
5. Common Mistakes
6. Practical Applications
1. Password Generator:
import random
import string
def generate_password(length=8):
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(length))
7. Key Takeaways