Experiment No.
- 02
Aim: To develop Python to generate random sequences.
Software: Python 3.13 as Interpreter and PyCharm as Integrated Development Environment.
Theory: Random numbers are numbers that occur in a sequence such that the values are unpredictable
and appear to follow no specific pattern. The use of random numbers is fundamental in various
domains such as statistical sampling, simulations, games, cryptography, procedural content generation,
machine learning and randomized algorithms. Random numbers help model unpredictable processes
such as weather, traffic, or queueing systems. In cryptography, secure random numbers are essential
for key generation and encryption. In data science, they are used to split datasets, generate synthetic
data or select random samples.
The generation and application of pseudo-random sequences in Python using the random module.
A list of ten student names is used as the base dataset. The goal is to randomly shuffle this list using
two possible seed options: a user-defined seed or a system-generated seed based on current time.
The logic of the program begins by importing the required modules: random for shuffling and
time for obtaining the current timestamp. The user is presented with two options to choose the
seeding method. If the user provides a specific seed, it ensures that the same output is obtained each
time the program is executed with that seed. If the system time is used as the seed, a different output
is produced on each execution, simulating randomness more effectively. The random.shuffle()
function is used to rearrange the student names in place based on the generated seed.
Flow Chart: A flowchart is a visual representation of the sequence of steps involved in a process
or algorithm. It uses standardized symbols such as ovals for start/end, rectangles for processes,
parallelograms for input / output, and arrows to show the flow of control. Flowcharts help in
understanding, analyzing and debugging logic before actual coding. They are especially useful in
planning and communicating the structure of a program.
1
Python Programming Lab Random Sequences Generator
Start
Display menu options
Invalid input? Invalid 1
User choice? Input user seed
Use default
Use system time as seed
Shuffle student list
Display arranged list
End
Program:
import random # For generating random sequences
import time # For using system time as a seed
# List of student names
students = ["Aman", "Bhavna", "Chirag", "Divya", "Esha",
"Farhan", "Gauri", "Harsh", "Isha", "Jay"]
# Display menu to user
print("Random Sequence Generator")
print("1. Use user-defined seed") # Option 1: User sets seed
Dr. D. K. Singh 2 National Fire Service College, Nagpur
Random Sequences Generator Python Programming Lab
print("2. Use system time as seed") # Option 2: Use system time
mode = input("Enter choice (1 or 2): ")
# Set the seed for random based on user input
if mode == ’1’:
user_seed = int(input("Enter a seed value (integer): "))
# Get user-defined seed
random.seed(user_seed) # Set the seed
print(f"Seed set to: {user_seed}")
elif mode == ’2’:
system_seed = int(time.time()) # Get current time in seconds
random.seed(system_seed) # Set system time as seed
print(f"System time used as seed: {system_seed}")
else:
print("Invalid choice! Defaulting to system time.")
random.seed() # If input is invalid, still proceed
# Randomly rearrange the student list
random.shuffle(students)
# Print the shuffled student list
print("\nRandomly arranged students:")
for i, name in enumerate(students, start=1):
print(f"{i}. {name}")
Program Output: Random Sequences Generator
Random Sequence Generator
1. Use user-defined seed
2. Use system time as seed
Enter choice (1 or 2): 1
Enter a seed value (integer): 5
Seed set to: 5
National Fire Service College, Nagpur 3 Dr. D. K. Singh
Python Programming Lab Random Sequences Generator
Randomly arranged students:
1. Chirag
2. Divya
3. Bhavna
4. Aman
5. Isha
6. Harsh
7. Gauri
8. Farhan
9. Esha
10. Jay
Dr. D. K. Singh 4 National Fire Service College, Nagpur