NumPy Random
Submitted by moazkhan on Sunday, June 21, 2020 - 14:37.
In this tutorial you will learn:
- What are random numbers and how are they generated?
- NumPy randint() function
- NumPy rand() function
Random Numbers
Random numbers being generated by a computer seems to be an illogical phenomenon as the computers are always predictable. In world of computing, the mathematical formulae are basically behind the random numbers being generated, they always follow a logical sequence, not influenced by any external source and are termed as pseudo random. True Random is another term related to generating the random numbers, it follow an illogical sequence while generating random numbers and in this case the number being generated is always dependent on the external source, independent source could be mouse clicks, strength of internet signals etc.
Randint() Function
Random is a module in NumPy which generates pseudo random numbers. In order to use the module we have to first import NumPy and then we have to import Random. Lets take an example, in this example we will generate an integer random number between 0 and 20 using function randint(). As the randint() name suggests, it is a function which generate only integer DataType random numbers and the function takes an argument 20, which indicates that the numbers will be generated from 0 to 20. Here you can see that we are generating the random number twice and both the times we are getting a different result. The function rantint() takes in one mandatory argument (the upper limit), and three optional argument which include lower limit, total random numbers to be generated and DataType.- from numpy import random as r
- #setting the limit to 20
- res1 = r.randint(20)
- #generating 3 random numbers of data type int, between 1 and 100
- res2 = r.randint(1,100,3,int)
- print('First random number is:',res1)
- print('Array of random number is:',res2)
- from numpy import random as r
- #setting the limit to 200 and size of the array equal to 5
- res = r.randint(200,size=(20))
- #displaying the result
- print('Array of random numbers is:',res)
- from numpy import random as r
- #setting the limit to 30 and shape of the array equal to 2,4
- res = r.randint(30,size=(2,4))
- #displaying the result.
- print('2D Array of random numbers is:',res)
rand() function
As randint() function only generates random numbers of DataType int, the rand() function generates random numbers of DataType float with all the values between zero and one. Lets take an example, in this example we will generate five float Data Type random numbers having values between zero and one.- from numpy import random as r
- #specifying the number of random numbers i.e equal to 5
- res = r.rand(5)
- #displaying the result.
- print('Array of random numbers is:',res)
- from numpy import random as r
- #specifying the dimension of 2D array (5,6)
- res = r.rand(5,6)
- #displaying the result.
- print('2D Array of float DataType random numbers is:',res)
Add new comment
- 134 views