rand, randn, randint functions

Welcome to the tutorial of Python



In this tutorial we will try to understand the differences between the 3 functions

These function are generally used to generate random numbers.

1) rand() function

  • range is between 0-1
  • follows uniform distribution
  • Useful when plotting large number of random points
  • Mean = 0.5

      Syntax: 
                    import numpy as np
                    np.random.rand(3)  #Output = arr([0.0021,0.1234,0.9764])
                    np.random.rand(2,2) #Output is 2D array(2x2) with random numbers between 0-1

2) randn() function

  • range is between -infinity to +infinity
  • Follows normal distribution
  • Mean is 0 and Standard Deviation is 1
      Syntax: 
                    np.random.randn(2) #Output = arr([0.1724,-2.2448])

3) randint() function - generate only integral values
      Syntax: np.random.randint(1,100) #Output = any number will be generated between 1(inclusive)                                                                  and 100(exclusive) i.e. 100 will never come

                   np.random.randint(1,100,10) #Output = array([60,40,23,47,89,73,66,21,28,98])



rand, randn, randint functions rand, randn, randint functions Reviewed by Analytics Pundit on November 30, 2019 Rating: 5

No comments:

Theme images by lishenjun. Powered by Blogger.