Saturday, September 21, 2024
Google search engine
HomeLanguagesRandom sampling in numpy | randint() function

Random sampling in numpy | randint() function

numpy.random.randint() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random integers from low (inclusive) to high (exclusive), i.e. in the interval [low, high).

Syntax : numpy.random.randint(low, high=None, size=None, dtype=ā€™lā€™)

Parameters :
low : [int] Lowest (signed) integer to be drawn from the distribution.But, it works as a highest integer in the sample if high=None.
high : [int, optional] Largest (signed) integer to be drawn from the distribution.
size : [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
dtype : [optional] Desired output data-type.

Return : Array of random integers in the interval [low, high) or a single such random int if size not provided.

Code #1 :




# Python program explaining
# numpy.random.randint() function
Ā Ā 
# importing numpy
import numpy as geek
Ā Ā 
# output array
out_arr = geek.random.randint(low = 0, high = 3, size = 5)
print ("Output 1D Array filled with random integers : ", out_arr)Ā 


Output :

Output 1D Array filled with random integers :  [1 1 0 1 1]

Ā 

Code #2 :




# Python program explaining
# numpy.random.randint() function
Ā Ā 
# importing numpy
import numpy as geek
Ā Ā 
Ā Ā 
# output array
out_arr = geek.random.randint(low = 4, size =(2, 3))
print ("Output 2D Array filled with random integers : ", out_arr)Ā 


Output :

Output 2D Array filled with random integers :  [[1 1 0]
 [1 0 3]]

Ā 
Code #3 :




# Python program explaining
# numpy.random.randint() function
Ā Ā 
# importing numpy
import numpy as geek
Ā Ā 
# output array
out_arr = geek.random.randint(2, 10, (2, 3, 4))
print ("Output 3D Array filled with random integers : ", out_arr)Ā 


Output :

Output 3D Array filled with random integers :  [[[4 8 5 7]
  [6 5 6 7]
  [4 3 4 3]]

 [[2 9 2 2]
  [3 2 2 3]
  [6 8 3 2]]]
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?