What is NumPy Random Intro | NumPy Tutorial
NumPy Random Intro|NumPy Tutorial
Something that cannot be predicted logically is termed as Random. Python doesn’t have any random() function to generate random numbers, but it has random modules that work to generate random numbers.
There are two types of Random Number
Random numbers generated through an algorithm are called Pseudo Random.
2. True Random:
To generate a truly random number on our computer we need to take random data from outside sources. These outside sources are our keystrokes, mouse movements,
Data on network etc.
Python provides a module termed as random to generate random numbers. So before using this we need to install & import the random package.
This module is very useful and various applications of randomness have led to the development of several different methods for generating random data.
Randomness has many uses in science, art, statistics, cryptography, gaming, gambling, and many other fields.
The syntax for installation:
pip install random
The syntax for importing:
from numpy import random
Syntax for random.randint:
This returns random numbers between low(inclusive) and high(exclusive).
Low: integer or an array-like ints
High: integer or an array-like of ints, optional
Size: integer or tuple of ints, optional
Dtype: desired datatype of the result, optional
It returns int or ndarray of ints.
Now, let's see the Example code to check how numpy.random.randint() works.
#code to generate random numbers between 0 to 20 (excluding 20) import numpy as np from numpy import random random_num = np.random.randint(2, size=20) print(random_num) #output:[1 1 1 0 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1]
#code to generate 3x4 arrays of ints between 0 and 6 import numpy as np from numpy import random arr =np.random.randint(6, size=(3,4)) print(arr) #output: [[5 1 3 0] [0 0 4 1] [2 2 2 4]]
Thanks for reading this article, in an upcoming article you will learn more about the random module in python.