How to use Python Numpy to generate Random Numbers?

PythonServer Side ProgrammingProgramming

The random module in Numpy package contains many functions for generation of random numbers

numpy.random.rand() − Create an array of the given shape and populate it with random samples

>>> import numpy as np
>>> np.random.rand(3,2)
array([[0.10339983, 0.54395499],
[0.31719352, 0.51220189],
[0.98935914, 0.8240609 ]])

numpy.random.randn() − Return a sample (or samples) from the “standard normal” distribution.

>>> np.random.randn()
-0.6808986872330651

numpy.random.randint() − Return random integers from low (inclusive) to high (exclusive).

>>> np.random.randint(5, size=(2, 4))
array([[2, 4, 0, 4],
[3, 4, 1, 2]])

numpy.random.random() − Return random floats in the half-open interval [0.0, 1.0).

>>> np.random.random_sample()
0.054638060174776126

raja
Published on 05-Mar-2018 18:52:11
Advertisements