Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

4 Ways to Perform Random Sampling in NumPy

  • Safa MulaniSafa Mulani
  • March 31, 2021
  • NumPy, Python Programming
4 Ways to Perform Random Sampling in NumPy

Hello, readers! In this article, we will be focusing on 4 Easy Ways to Perform Random Sampling in Python NumPy.

So, let us get started! 🙂

Random Sampling, to give an overview, is actually selecting random values from the defined type of data and present them to be in further use.

In the course of this topic, we will be having a look at the below functions–

  1. NumPy random_sample() method
  2. NumPy ranf() method
  3. NumPy random_integers() method
  4. NumPy randint() method

1. NumPy random_sample() method for Random Sampling

With random_sample() method, we can sample the data values and choose random data fat ease. It selects random samples between [0.0 – 1.0] only. We can build a single sample as well as an entire array based on random values.

Have a look at the below syntax!

random.random_sample()

Example:

In the below example, at first, we have performed random sampling and generated a single random value. Further, we have created a 2-dimensional array with random values by passing size as a parameter to the random_sample() function.

Note it that the random values would range between 0.0 to 1.0 only. Plus, random_sample() function generates random values of float type.

import numpy as np

ran_val = np.random.random_sample()
print ("Random value : ", ran_val)

ran_arr = np.random.random_sample(size =(2, 4))
print ("Array filled with random float values: ", ran_arr) 

Output:

Random value :  0.3733413809567606
Array filled with random float values:  [[0.45421908 0.34993556 0.79641287 0.56985183]
                                        [0.88683577 0.91995939 0.16168328 0.35923753]]

2. The random_integers() function

With random_integers() function, we can generate random values or even a multi-dimensional array of random value of type integer. That it, it generates random values of type Integer. Further, it gives us the liberty to choose the range of integer values from which the random numbers would be selected.

Syntax:

random_integers(low, high, size)
  • low: The lowest scale/limit for the random values to be chosen. The random values would not have a value below the low value mentioned.
  • high: The highest scale/limit for the random values to be chosen. The random values would not have a value beyond the high value mentioned.
  • size: The number of rows and columns for the array to be formed.

Example:

In this example, we have created a single dimensional random valued array having values between the range 5-10 only. Further, we have set up a multi-dimensional array using the same concept.

import numpy as np

ran_val = np.random.random_integers(low = 5, high =10 , size = 3)
print ("Random value : ", ran_val)

ran_arr = np.random.random_integers(low = 5, high =10 , size = (2,4))
print ("Array filled with random float values: ", ran_arr) 

Output:

Random value :  [10  5  9]
Array filled with random float values:  [[ 8  8  9  6]
                                        [ 6 10  8 10]]

3. The randint() function

The randint() function works in a similar fashion as that of random_integers() function. It creates an array having random values within the specified range of integers.

Example:

import numpy as np

ran_val = np.random.randint(low = 5, high =10 , size = 3)
print ("Random value : ", ran_val)

Output:

Random value :  [5 8 9]

4. The ranf() function

Again, ranf() function resembles random_sample() method in terms of functioning. It generates random numbers of type float between 0.0 to 1.0 only.

Example:

import numpy as np

ran_val = np.random.ranf()
print ("Random value : ", ran_val)

Output:

Random value :  0.8328458165202546

Conclusion

Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, Stay tuned with us! Till then, Happy Learning! 🙂

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
5 NumPy Data Distributions to know
Previous Post 5 NumPy Data Distributions to know
Next Post NumPy Linear Algebraic functions to know!
NumPy Linear Algebraic functions to know!

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
What Does %s Mean in Python String Formatting?

What Does %s Mean in a Python Format String?

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.