Pseudo Random and True Random |
random number does NOT mean a different number every time random means something that can not be predicted logically |
Generate Random Number |
NumPy offers the random module to work with random numbers
generate a random integer from 0 to 100
from numpy import random x = random.randint(100) print(x) |
Generate Random Float |
the random module's rand() method returns a random float between 0 and 1
from numpy import random x = random.rand() print(x) |
Generate Random Array |
Integers
can use the two methods above to make random arraysthe randint() method takes a size parameter specifying the shape of an array generate a 1-D array containing 5 random integers from 0 to 100 from numpy import random x=random.randint(100, size=(5)) print(x) |
Generate Random Number From Array |
the choice() method allows you to generate a random value based on an array of values takes an array as a parameter and randomly returns one of the values from numpy import random x = random.choice([3, 5, 7, 9]) print(x)method can also return an array of values size parameter can specify the shape of the array generate a 2-D array that consists of the values in the array parameter from numpy import random x = random.choice([3, 5, 7, 9], size=(3, 5)) print(x)set replace parameter to False to keep duplicates out of the result arr = np.random.choice(arr, size=(20), replace=False) |