Repository navigation
Expand file tree
/
Copy pathrandomT.py
More file actions
50 lines (29 loc) · 2.35 KB
/
Copy pathrandomT.py
File metadata and controls
50 lines (29 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ===================== SUMMARY OF random MODULE =====================
# random module is used to generate random numbers and pick random items.
# random.random() gives float between 0 and 1.
# randint(), randrange(), uniform() generate random numbers in ranges.
# choice(), choices(), sample() select random values from a sequence.
# shuffle() changes the order of a list randomly.
# seed() is used to get the same random output every time for practice.
# ===================================================================
import random # importing the random module
random.seed(10) # setting seed so output remains same every time
print(random.random()) # gives random float between 0.0 and 1.0
print(random.randint(1, 10)) # gives random integer between 1 and 10, both included
print(random.randrange(1, 10)) # gives random integer from 1 to 9, 10 excluded
print(random.randrange(2, 21, 2)) # gives random even number from 2 to 20
print(random.uniform(1, 5)) # gives random float between 1 and 5
names = ["Saad", "Aman", "Rahul", "Zoya"] # creating a list of names
print(random.choice(names)) # picks one random item from the list
print(random.choices(names, k=3)) # picks 3 random items, repetition allowed
print(random.sample(names, 2)) # picks 2 random items, repetition not allowed
numbers = [10, 20, 30, 40, 50] # creating a list of numbers
random.shuffle(numbers) # shuffles the list randomly in-place
print(numbers) # printing shuffled list
print(random.getrandbits(4)) # gives random integer using 4 random bits
otp = random.randint(1000, 9999) # generating a 4-digit random OTP
print("Your OTP is:", otp) # printing the generated OTP
password_chars = ["A", "B", "C", "1", "2", "@"] # creating characters for random password
password = random.choices(password_chars, k=6) # selecting 6 random characters, repetition allowed
password = "".join(password) # joining list characters into one string
print("Password:", password) # printing random password