NumPy Random

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.
  1. from numpy import random as r
  2. #setting the limit to 20
  3. res1 = r.randint(20)
  4. #generating 3 random numbers of data type int, between 1 and 100
  5. res2 = r.randint(1,100,3,int)
  6. print('First random number is:',res1)
  7. print('Array of random number is:',res2)
Lets take another example, in this example we have used a parameter “size” and passed it as an argument to randint() function.
  1. from numpy import random as r
  2. #setting the limit to 200 and size of the array equal to 5
  3. res = r.randint(200,size=(20))
  4. #displaying the result
  5. print('Array of random numbers is:',res)
The randint() function also gives a flexibility of generating 2D array of a specified dimension. As we can see in this example we have made an array of dimension(2,4) of 8 random numbers below 30.
  1. from numpy import random as r
  2. #setting the limit to 30 and shape of the array equal to 2,4
  3. res = r.randint(30,size=(2,4))
  4. #displaying the result.
  5. 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.
  1. from numpy import random as r
  2. #specifying the number of random numbers i.e equal to 5
  3. res = r.rand(5)
  4. #displaying the result.
  5. print('Array of random numbers is:',res)
Just like in function randint(), we can generate random numbers as constituent of a 2D array. Lets take an example, here you can see that we are generating an array having dimension of (5,6) having float Data type random numbers between zero and one.
  1. from numpy import random as r
  2. #specifying the dimension of 2D array (5,6)
  3. res = r.rand(5,6)
  4. #displaying the result.
  5. print('2D Array of float DataType random numbers is:',res)

Add new comment