-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_examples.py
81 lines (61 loc) · 1.86 KB
/
random_examples.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import random
# random float in [0,1)
a = random.random()
print(a)
# random float in range [a,b]
a = random.uniform(1,10)
print(a)
# random integer in range [a,b]. b is included
a = random.randint(1,10)
print(a)
# random integer in range [a,b). b is excluded
a = random.randrange(1,10)
print(a)
# random float from a normal distribution with mu and sigma
a = random.normalvariate(0, 1)
print(a)
# choose a random element from a sequence
a = random.choice(list("ABCDEFGHI"))
print(a)
# choose k unique random elements from a sequence
a = random.sample(list("ABCDEFGHI"), 3)
print(a)
# choose k elements with replacement, and return k sized list
a = random.choices(list("ABCDEFGHI"),k=3)
print(a)
# shuffle list in place
a = list("ABCDEFGHI")
random.shuffle(a)
print(a)
import secrets # TRUELY RANDOM FOR SECURITY
# random integer in range [0, n).
a = secrets.randbelow(10)
print(a)
# return an integer with k random bits.
a = secrets.randbits(5)
print(a)
# choose a random element from a sequence
a = secrets.choice(list("ABCDEFGHI"))
print(a)
# random arrays installed numpy using pip
import numpy as np
np.random.seed(1)
# rand(d0,d1,…,dn)
# generate nd array with random floats, arrays has size (d0,d1,…,dn)
print(np.random.rand(3))
# reset the seed
np.random.seed(1)
print(np.random.rand(3))
# generate nd array with random integers in range [a,b) with size n
values = np.random.randint(0, 10, (5,3))
print(values)
# generate nd array with Gaussian values, array has size (d0,d1,…,dn)
# values from standard normal distribution with mean 0.0 and standard deviation 1.0
values = np.random.randn(5)
print(values)
# randomly shuffle a nd array.
# only shuffles the array along the first axis of a multi-dimensional array
# instead of random.seed() use np.random/seed() to prove numpy isn't really random
arr = np.array([[1,2,3], [4,5,6], [7,8,9]])
np.random.shuffle(arr)
print(arr)