NumPy random.uniform(): generate a random sample from a uniform distribution

Einblick Content Team - March 20th, 2023

At times, it is necessary to introduce randomness according to a particular distribution when implementing various machine learning models. In this post, we’ll examine how to use the NumPy’s random.uniform() function, which will generate n values from a uniform distribution within a specified range.

Basic Syntax: np.random.uniform(low, high, size)

from numpy.random import default_rng


# Instantiate NumPy Generator instance, setting random seed
rng = default_rng(seed = 1)


# Sample 1: [0, 1), 200 numbers
sample1 = rng.uniform(low = 0, high = 1, size = 200)
print("Sample 1:")
print(sample1)


# Sample 2: [-10, 100), 200 numbers
sample2 = rng.uniform(low = -10, high = 10, size = 200)
print("Sample 2:")
print(sample2)

Output:

Sample 1:
[0.1719594  0.22005712 0.6320633  0.58757608 0.36199078 0.06035538
 0.19761037 0.65092953 0.23661011 0.62843592 0.32415973 0.45364117
 ...]
 
Sample 2:
[-3.42699535  5.59605115 -0.21797547 -0.50691541 -9.76982705 -1.19263974  -1.5363445   7.52296261  2.43838142 -3.51784328
 ...]

NOTE: since NumPy version 1.17.0, it has been recommended to first instantiate a Generator, such as default_rng(), and then call the numpy.random.uniform() function on the generator object. In this case, we've set the random seed for the Generator so that the results are reproducible.

The uniform() function takes three arguments:

  • low: the lowest value that can be generated, NumPy’s range for the function is inclusive of this value
  • high: the upper boundary for the function, NumPy’s range for the function is exclusive of this value
  • size: the number of values to generate

If we use seaborn's histplot() function to create histograms of the distribution of our two samples, we'll see that they are roughly uniform, as expected.

Sample 1: np.random.uniform(low = 0, high = 1, size = 200)

NumPy random.uniform() example 1 histogramNumPy random.uniform() example 1 histogram

Sample 2: np.random.uniform(low = -10, high = 10, size = 200)

NumPy random.uniform() example 2 histogramNumPy random.uniform() example 2 histogram

From the above plots, we can see the distributions are roughly uniform!

BONUS: Try using Generative AI

We've launched our AI agent, Einblick Prompt--which can write and fix code, create beautiful charts, build models, and much more--into every one of our AI-native data notebooks. Prompt is powered by OpenAI and LangChain, but tailored for the data domain, so you can ask Prompt to complete data tasks, like generating synthetic data, for you. Then, you can run the code immediately since our notebooks operate like a Jupyter notebook, with a Python kernel. You can connect to external data sources like Snowflake or Databricks, and even run SQL code too. Check out how we used Prompt below:

Using Generative AI in Einblick

  1. Open an Einblick canvas (you can fork the one below)
  2. Right click anywhere in the canvas > Select Prompt
  3. Type in: "Generate 200 random uniformly distributed values between 0 and 1, and create a histogram of the values."
  4. Select the Python cell > Shift + Enter to run the code

Test out different prompts, and get results in seconds!

About

Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.