-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path40_Random_module.py
More file actions
65 lines (43 loc) · 1.15 KB
/
40_Random_module.py
File metadata and controls
65 lines (43 loc) · 1.15 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Python Random module generates random numbers in Python. It introduce randomness into programs."""
"""Examples of Random Module"""
# Example 1: Pick a Random Element from a List
import random
a = [1, 2, 3, 4, 5, 6]
print(random.choice(a))
# Example 2: Using seed() for Reproducible Output
import random
random.seed(5)
print(random.random())
print(random.random())
# Example 3: Generate Random Integers in a Range
import random
r1 = random.randint(5, 15)
print(r1)
r2 = random.randint(-10, -2)
print(r2)
# Example 4: Generate a Random Float Between 0 and 1
from random import random
print(random())
# Example 5: Randomly Select from List, String, and Tuple
import random
a = [1, 2, 3, 4, 5, 6]
print(random.choice(a))
s = "geeks"
print(random.choice(s))
tup = (1, 2, 3, 4, 5)
print(random.choice(tup))
# Example 6: Select Multiple Unique Random Items
from random import sample
a = [1, 2, 3, 4, 5]
print(sample(a,3))
b = (4, 5, 6, 7, 8)
print(sample(b,3))
c = "Akhil"
print(sample(c,3))
# Example 7: Shuffle Elements in a List
import random
a = [1, 2, 3, 4, 5]
random.shuffle(a)
print("After shuffle : ",a)
random.shuffle(a)
print("\nSecond shuffle : ",a)