Skip to content

Commit d99c15a

Browse files
Random Module
1 parent 5a92af4 commit d99c15a

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

Random_module part 1.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#;==========================================
2+
#; Title: Python Random module part 1
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
6+
7+
import random as r
8+
9+
#random integer
10+
print(r.randint(1,100))
11+
print(r.randint(1,100))
12+
print(r.randint(1,100))
13+
14+
#randrange
15+
print(r.randrange(10,100,5))
16+
print(r.randrange(10,100,5))
17+
print(r.randrange(10,100,5))
18+
19+
#random floating numbers
20+
print(r.random())
21+
print(r.random())
22+
23+
#random floating within given range
24+
print(r.uniform(5,10))
25+
print(r.uniform(5,10))
26+
print(r.uniform(5,10))
27+
28+
#random.choice() example
29+
30+
list_1=[1,2,3,4,5,6,7,8,9]
31+
32+
print(r.choice(list_1)) #This choice is not cryptographically secured
33+
print(r.choice(list_1))
34+
print(r.choice(list_1))
35+
36+
#choice in dictionary
37+
dictt={
38+
"Kelly": 50,
39+
"Red": 68,
40+
"Jhon": 70,
41+
"Emma" :40
42+
}
43+
44+
key=r.choice(list(dictt))
45+
value=dictt[key]
46+
print(f"{key} : {value}")
47+
48+
49+
#let's chose same element every time : by help of seed method
50+
print("Now we are selecting the same element everytime : ")
51+
r.seed(4)
52+
key=r.choice(list(dictt))
53+
value=dictt[key]
54+
print(f"1 - {key} : {value}")
55+
56+
r.seed(4)
57+
key=r.choice(list(dictt))
58+
value=dictt[key]
59+
print(f"2 - {key} : {value}")
60+
61+
r.seed(4)
62+
key=r.choice(list(dictt))
63+
value=dictt[key]
64+
print(f"3 - {key} : {value}")
65+
66+
r.seed(4)
67+
key=r.choice(list(dictt))
68+
value=dictt[key]
69+
print(f"4 - {key} : {value}")

Random_module part 2.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#;==========================================
2+
#; Title: Python Random module part 2
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
6+
import random as r
7+
8+
multi_list=[[1,2,3,4],[5,6,4,2],[6,5,4,8],[9,8,7,441]]
9+
print(r.choice(multi_list))
10+
11+
#multiple choices :
12+
sample_list=[1,2,3,4,5,6,47,28,76,85,74,78]
13+
print(r.choices(sample_list,k=5)) #5 choices among them
14+
15+
#choose from set :
16+
set1={20, 35, 45, 65, 82}
17+
print(r.choice(tuple(set1))) #you need to convert set in tuple
18+
19+
#sample modules :
20+
sample_list=[1,2,3,4,5,6,47,28,76,85,74,78]
21+
sl=r.sample(sample_list,k=5) #it will make a sample of k number of element from the sample list
22+
print(sl)
23+
24+
sample_set={1,2,3,4,5,6,47,28,76,85,74,78} #lets make a sample from set
25+
print(r.sample(sample_set,k=4))
26+
27+
sample_dict = { "Kelly": 50, "Red": 68, "Jhon": 70, "Emma" :40 }
28+
print(r.sample(list(sample_dict),k=3)) #return the keys in a sample list
29+
30+
#another way to do this
31+
32+
dictt={
33+
"Kelly": 50,
34+
"Red": 68,
35+
"Jhon": 70,
36+
"Emma" :40,
37+
"Ashik" : 79,
38+
"Musleh": 99,
39+
"Lia": 100,
40+
"Miskat": 66,
41+
"Jacce": 90
42+
}
43+
44+
print(r.sample(dictt.items(),k=5)) #return a list of key and value
45+
46+
#so what's difference between choice and sample :
47+
#sample makes the random sample of given iter
48+
#choice makes random choice of given iter with repeated value
49+
#let's seeeeeeeeee
50+
sample_list=[1,2,3,4,5,6,47,28,76,85,74,78]
51+
print(f"Always Unique : {r.sample(sample_list,k=5)}")
52+
print(f"Some time repeted : {r.choices(sample_list,k=5)}")

Random_module part 3.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#;==========================================
2+
#; Title: Python Random module part 3
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
import random as r
6+
7+
8+
#whole random module is controled by seed value
9+
r.seed(0)
10+
print(r.randint(0,500))
11+
r.seed(0)
12+
print(r.randint(0,500))
13+
r.seed()
14+
###let's shuffling a string
15+
name="ashik"
16+
l_n=list(name)
17+
r.shuffle(l_n)
18+
name=''.join(l_n)
19+
print(name)
20+
#shuffle second time
21+
l_n=list(name)
22+
r.shuffle(l_n)
23+
name=''.join(l_n)
24+
print(name)
25+
26+
27+
#let's control shuffling by seed
28+
name="ashik"
29+
l_n=list(name)
30+
r.seed(0)
31+
r.shuffle(l_n)
32+
name=''.join(l_n)
33+
print(name)
34+
35+
name="ashik"
36+
l_n=list(name)
37+
r.seed(0)
38+
r.shuffle(l_n)
39+
name=''.join(l_n)
40+
print(name)
41+
r.seed()
42+
43+
44+
#note: you can actually do the shuffle with the help of sample
45+
lis=[1,2,3,4,5]
46+
r.seed() #when you need to clear seed value from others then just type it :
47+
look_like_shuffle=r.sample(lis,len(lis))
48+
print(look_like_shuffle)
49+
50+
#shuffle two python list at once with same order :
51+
empName = ['Jhon', 'Emma', 'Kelly', 'Jason']
52+
empSalary = [7000, 6500, 9000, 10000]
53+
suf_both=list(zip(empName,empSalary))
54+
r.shuffle(suf_both)
55+
s_name,s_salary=zip(*suf_both)
56+
s_name,s_salary=list(s_name),list(s_salary)
57+
print(s_name,s_salary)
58+
59+
#generate random float number with nth of precision
60+
print(round(r.uniform(0.1,5.0),5))
61+
print(round(r.uniform(0.1,5.0),3))
62+
print(round(r.uniform(0.1,5.0),9))

0 commit comments

Comments
 (0)