Learn Python

Learn Data Structure & Algorithm

Numpy Introduction

Numpy Array

Numpy Attributes

Numpy generate random numbers

Numpy Slicing

Numpy Concatenate & split

Numpy Insert & delete

Numpy Math

Numpy Condition

Numpy Reshaping

Learn Pandas

Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Generate random numbers using numpy

random.randint() function

random.randint() generates an array that contains random numbers. In the bracket first value is for the starting point of the number, the second value is for the ending point of the number and the third value is for how many numbers you want between starting and ending points. So this function will create an array which will contain random numbers according to the given parameters. Each time you execute the code will generate new numbers.

Example:

Input
import numpy as np
x=np.random.randint(1,15,4)
print(x)
Output
[4 6 13 14]

random.ranf() function

It will generate float random values in an array between 0.0 to 1.0. So we can say that this function will create an array that will contain random float values between 0.0 to 1.0. In the bracket of the function, we have to pass how many random float numbers we want.

Example:

Input
import numpy as np
x=np.random.ranf(5)
print(x)
Output
[0.77731545 0.35346582 0.1070274 0.47867248 0.34930472]

random.rand() function

random.rand() function generates numbers between 0 to 1 and then creates an array. In the function bracket write row and column number. It means how many columns and rows you want in the array. In the bracket first value is for the row and the second value is for the column.

Example:

Input
import numpy as np
x=np.random.rand(2,2)
print(x)
Output
[[0.95656016 0.76420277 0.18812433]
[0.96036083 0.94452681 0.01973328]]

random.randn() function

random.randn() function generates negative and positive number near 0 and then creates an array. In the function bracket pass the row and column number. It means how many columns and rows you want in the array. In the bracket first value is for the row and the second value is for the column.

Example:

Input
import numpy as np
x=np.random.randn(1,2)
print(x)
Output
[[-0.95656016 0.76420277]]

seed() function

What happens while using random.ranint() function is, every time you refresh the code, you will get new numbers. But if you want the same number every time then before random.ranint() use random.seed() function and inside of the bracket function, pass some random numbers according to your mood, and for this function, you will get the same numbers every time.

Example:

Input
import numpy as np
np.random.seed(123)
x=np.random.randint(10,40,10)
print(x)
Output
[12 15 19 24 27 30 33 35 38 40 ]

random.choice() function

random.choice() function randomly choose one value from a random number.The random number can come from an array, list, or random.randint function etc.

Example:

Input
import numpy as np
x=[1,2,3,4,5]
y=np.random.choice(x)
print(y)
Output
4

random.permutation() function

random.permutation() function rearrange the index number of the values present in the array.

Example:

Input
import numpy as np
x=[1,2,3,4,5]
y=np.random.permutation(x)
print(y)
Output
[3 4 5 1 2]

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.