xxxxxxxxxx
import numpy as np
# Set the seed value
np.random.seed(42)
# Generate random numbers
random_numbers = np.random.rand(5)
print(random_numbers)
xxxxxxxxxx
import numpy as np
np.random.seed(42)
random_numbers = np.random.random(size=4)
random_numbers
xxxxxxxxxx
>>> import numpy as np
>>>
>>> np.random.seed(0)
>>>
>>> np.random.rand(3)
array([0.5488135 , 0.71518937, 0.60276338])
>>> np.random.rand(3)
array([0.54488318, 0.4236548 , 0.64589411])
>>>
>>>
>>> np.random.seed(1)
>>>
>>> np.random.rand(3)
array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04])
>>> np.random.rand(3)
array([0.30233257, 0.14675589, 0.09233859])
>>>
>>>
>>> np.random.seed(0)
>>>
>>> np.random.rand(3)
array([0.5488135 , 0.71518937, 0.60276338])
>>> np.random.rand(3)
array([0.54488318, 0.4236548 , 0.64589411])
xxxxxxxxxx
# If you want to generate pseudo-random numbers use numpy.random.seed()
# The syntax is "np.random.seed(seed_value)"
# seed_value is any integer equal to or more than 0
# You will get different results when you use a different integers
import numpy as np
np.random.seed(0)
np.random.random()
# Output - 0.5488135039273248
np.random.seed(1)
np.random.random()
# Output - 0.417022004702574
# But if you try a "random" function without a seed, the output will be different
# every time we run the code
# It will be a True-random number
np.random.random()
# Output will be a random number