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

  1. Start
  2. Import the random module
  3. Generate first random number between 1 and 100 for customer 1
  4. Generate second random number between 1 and 100 for customer 2
  5. Generate third random number between 1 and 100 for customer 3
  6. Generate fourth random number between 1 and 100 for customer 4
  7. Display all four lucky winner customer numbers
  8. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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

Need Help?

Join our tuition classes for personalized guidance and doubt clearing sessions.

Register for Classes →

Practice More

Try more Python programs to strengthen your programming skills.

View All Programs →