Class 11 Practical Programs
Choose 4 Random Lucky Winners from 100 Customers
AIM
To write a Python program that randomly selects 4 customers as lucky winners from a pool of 100 customers using the random module.
ALGORITHM
- Start
- Import the random module
- Generate first random number between 1 and 100 for customer 1
- Generate second random number between 1 and 100 for customer 2
- Generate third random number between 1 and 100 for customer 3
- Generate fourth random number between 1 and 100 for customer 4
- Display all four lucky winner customer numbers
- Stop
PROGRAM
# Method 1: Basic approach (may have duplicates)
import random
c1 = random.randint(1, 100)
c2 = random.randint(1, 100)
c3 = random.randint(1, 100)
c4 = random.randint(1, 100)
print("Lucky winners are:", c1, c2, c3, c4)
# Method 2: Using random.sample() to avoid duplicates
import random
customers = list(range(1, 101)) # Create list of customers 1-100
lucky_winners = random.sample(customers, 4)
print("Lucky winners are:", lucky_winners)
# Method 3: Using set to ensure unique winners
import random
winners = set()
while len(winners) < 4:
winner = random.randint(1, 100)
winners.add(winner)
print("Lucky winners are:", list(winners))OUTPUT
Method 1 Output:
Lucky winners are: 23 67 45 89
Method 2 Output:
Lucky winners are: [12, 78, 34, 91]
Method 3 Output:
Lucky winners are: [56, 23, 87, 14]
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
What is the purpose of the random module in Python?
The random module provides functions to generate random numbers, make random selections, and shuffle sequences. It's useful for simulations, games, testing, and statistical sampling.
What is the difference between random.randint() and random.sample()?
random.randint(a, b) generates a single random integer between a and b (inclusive), while random.sample(population, k) selects k unique elements from a population without replacement.
Why might the basic method produce duplicate winners?
Since each random.randint() call is independent, there's a possibility that the same number could be generated multiple times, resulting in duplicate customer numbers.
How does random.sample() ensure unique winners?
random.sample() uses sampling without replacement, meaning once a number is selected, it cannot be selected again in the same operation, guaranteeing unique results.
What would happen if we try to sample more elements than available?
If we try random.sample(range(1, 101), 150), Python would raise a ValueError because we cannot sample 150 unique elements from a population of only 100.
How can we make the random selection reproducible?
We can use random.seed() with a specific value before generating random numbers. This ensures the same sequence of random numbers is generated each time the program runs.
What is the advantage of using a set in Method 3?
Sets automatically handle duplicates - if the same number is generated twice, it's only stored once. The while loop continues until we have exactly 4 unique winners.
How would you modify this program to select winners from a list of customer names?
We could create a list of customer names and use random.sample(customer_names, 4) to directly select 4 random names from the list.
Related Resources
- Python Fundamentals
- Python Input/Output Tutorial
- Python Basics PDF
- Python Fundamentals MCQs
- Next Program: Number Comparison
Need Help?
Join our tuition classes for personalized guidance and doubt clearing sessions.
Register for Classes →