Generating random numbers is a common task in many applications, such as simulations, cryptography, games, and data analysis.
NumPy is a popular library for scientific computing and data manipulation in Python. It provides a large collection of functions and methods for working with arrays, matrices, linear algebra, statistics, and more. One of the modules in NumPy is random, which contains functions for generating random numbers from various distributions and shuffling arrays.
Import Numpy as np
There are two main ways to generate random numbers in NumPy: using the randint function and using the rand function. Using the randint function.
The randint function generates random integers from a specified range.
The syntax of the function is:
The parameters are:
Low: The lower bound of the range (inclusive). It must be an integer or an array of integers.
High: The upper bound of the range (exclusive). It must be an integer or an array of integers. If not specified, it defaults to None, and the range becomes [0, low).
Size: The shape of the output array. It can be an integer or a tuple of integers. If not specified, it defaults to None, and a single integer is returned.
dtype: The data type of the output array. It must be a valid NumPy integer type. Int is used by default if it is not specified.
For example, to generate a single random integer between 0 and 10, we can use:
x = np. random. randint (10)
print(x)
# Output: 7
To generate a 1-D array of 5 random integers between 0 and 10, we can use:
x = np. random. randint (10, size=5)
print(x)
# Output: [9 4 0 1 5]
To generate a 2-D array of 3 rows and 4 columns, each containing a random integer between 0 and 10, we can use:
x = np. random. randint (10, size= (3, 4))
print(x)
# Output: [[3 6 7 8]
# [8 7 4 2]
# [0 9 6 9]]
The rand function generates random floats from a uniform distribution over [0, 1). The syntax of the function is:
The parameters are:
d0, d1, …, dn: the dimensions of the output array. They must be non-negative integers. One float is returned if no argument is provided.
For example, to generate a single random float between 0 and 1, we can use:
x = np. random. rand ()
print(x)
# Output: 0.764811614779789
To generate a 1-D array of 5 random floats between 0 and 1, we can use:
x = np. random. rand (5)
print(x)
# Output: [0.71597052 0.5426445 0.14217005 0.37334076 0.67413362]
To generate a 2-D array of 3 rows and 4 columns, each containing a random float between 0 and 1, we can use:
x = np. random. rand (3, 4)
print(x)
# Output: [[0.44183317 0.43401399 0.61776698 0.51313824]
# [0.65039718 0.60103895 0.8052232 0.52164715]
# [0.90864888 0.31923609 0.09045935 0.30070006]]
The random module also provides other functions for generating random numbers from different distributions, such as:
Normal: Creates arbitrary numbers using a Gaussian normal distribution.
Binomial: Makes use of a binomial distribution to produce random numbers.
Poisson: Produces arbitrary values based on a Poisson distribution.
Exponential: Makes use of an exponential distribution to produce random numbers.
Choice: Generates random samples from a given 1-D array.
Shuffle: Randomly shuffles the elements of a given array.
You can find more information about these functions and their parameters in the NumPy documentation.
Join our WhatsApp Channel to get the latest news, exclusives and videos on WhatsApp
_____________
Disclaimer: Analytics Insight does not provide financial advice or guidance. Also note that the cryptocurrencies mentioned/listed on the website could potentially be scams, i.e. designed to induce you to invest financial resources that may be lost forever and not be recoverable once investments are made. You are responsible for conducting your own research (DYOR) before making any investments. Read more here.