TECHMIND INFOTECH
Computer Education and Project Training Centre
(A Government Approved Institution)
Sri Ram Towers 2nd Floor, Santhanathapuram 6th Street, Pudukkottai
Cell: 7293 52 7293 Email:
[email protected] PYTHON PROGRAMMING – CLASS 19
NUMPY RANDOM
Generating a Random Integer Number
Example:
from numpy import random
x = random.randint(100)
print(x)
Output:
32
Generating Float Number from 0 to 1
Example:
from numpy import random
x = random.rand()
print(x)
Output:
0.4096553147594444
GENERATING RANDOM ONE DIMNESIONAL ARRAY
Example:
from numpy import random
x=random.randint(100, size=(5))
print(x)
Output:
[83 99 73 89 15]
GENERATING RANDOM THREE DIMENSIONAL ARRAY
Example:
from numpy import random
x = random.randint(100, size=(3, 5))
print(x)
Output:
[[80 54 19 74 65]
[26 60 69 34 25]
[50 16 53 84 90]]
GENERATING RANDOM FLOAT ONE DIMENSIONAL ARRAY
Example:
from numpy import random
x = random.rand(5)
print(x)
Output:
[0.9407526 0.7508614 0.8807069 0.1375779 0.9844576]
GENERATING RANDOM NUMBER FROM ARRAY
Example 1:
from numpy import random
x = random.choice([3, 5, 7, 9])
print(x)
Output:
5
Example 2:
from numpy import random
x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)
Output:
[[5 9 7 5 9]
[3 7 7 9 7]
[3 7 9 9 5]]
SETTING PROBABILITY
Example:
from numpy import random
x = random.choice([3, 5, 7, 9], p=[0.1, 0.9, 0.0, 0.0], size=(5))
print(x)
Output:
[5,3,5,5,5]
SHUFFLING ARRAYS
Example:
from numpy import random
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
random.shuffle(arr)
print(arr)
Output:
[5 2 3 4 1]
PERMUTATION
Example:
from numpy import random
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(random.permutation(arr))
Output:
[1 5 2 3 4]
SPLITTING ARRAY
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)
Output:
[array([1, 2]), array([3, 4]), array([5, 6])]