• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Random Uniform – Generate Random Numbers Uniformly in Range

Python Random Uniform – Generate Random Numbers Uniformly in Range

February 17, 2022 Leave a Comment

In Python, you can use the uniform() function from the Python random module to generate random numbers uniformly in a range.

import random

for x in range(0,5):
    print(random.uniform(0,10))

#Output:
9.142756405376938
4.285772552410724
8.395316881379259
2.3655937997142242
3.451488748786483

When working with data, it can be very useful to generate random numbers to be able to perform simulations or get a random sample of a dataset.

In Python, we can generate random numbers in a range easily. The Python random module has many useful functions for generating random numbers.

We can use the random.uniform() function to generate random floats in a range.

The random uniform() function takes in two arguments, the starting point of a range, and then ending point of a range, and returns a random float.

Below is an example of how to generate random floats in a range using the random module uniform() function.

import random

for x in range(0,5):
    print(random.uniform(0,10))

#Output:
9.142756405376938
4.285772552410724
8.395316881379259
2.3655937997142242
3.4514887487864835

How to Generate a Random Number Between Two Numbers in Python

We can easily generate random numbers in a range in Python with the uniform() function. As we know, the uniform() function takes two arguments and generates a random float between those two numbers.

Let’s create a function which will generate n random numbers in a given range and return them to us in a list.

Below is an example function in Python of how to generate n random numbers in a given range.

import random

def generateRandoms(a,b,n):
    randoms = []
    for x in range(0,n):
        randoms.append(random.uniform(a,b))
    return randoms

print(generateRandoms(5,15,10))
print(generateRandoms(0,100,10))
print(generateRandoms(-10,10,5))


#Output:
[5.694571539295616, 14.65143087945124, 8.281951034270097, 9.552800183834222, 14.466527725743468, 12.855545768024342, 5.555004019113447, 8.23846086476679, 13.68178655008311, 10.432019541766108]
[17.40931698981202, 21.338856311069076, 81.04739184068349, 9.05982067050396, 25.965253423876998, 50.77186986051261, 40.83043370119282, 22.9420759218404, 33.659027718483536, 87.5366194347588]
[1.0188556575830319, -0.3497662400817916, 0.8251588612524436, 1.098639882273325, -7.640642426128304]

How to Generate a Random Float Between 0 and 1 in Python

Generating a random number between 0 and 1 is easy in Python. We can use the uniform() function, or we can use the random() function.

The random() function generates a random floating point value between 0 and 1. This is equivalent to calling the random uniform() function with arguments 0 and 1.

Below is some code for how to use Python to generate random floating point numbers between 0 and 1.

import random

#Using random.random()
for x in range(0,3):
    print(random.random())

#Output:
0.5766391709474086
0.6620907660748007
0.8046101146489392

#Using random.uniform()
for x in range(0,3):
    print(random.uniform(0,1))

#Output:
0.11326338183229978
0.8906530419045259
0.2598350417177795

How to Generate a Random Integer in a Range Using Python

The last example I want to share with you in this article is how to generate a random integer in a range using Python.

You can use the random.uniform() function, but there is a function in the random module which generates a random integer in a range.

The randrange() function allows you to generate random integers in a range. Like the uniform() function, you pass two arguments which define a range, and the randrange() function returns random integers in that range.

Below is an example of how to generate random integers in Python with randrange().

import random

for x in range(0,5):
    print(random.randrange(0,10))

#Output:
1
0
4
0
9

Hopefully this article has been useful for you to learn how to generate random numbers in a range with random.uniform() in Python.

Other Articles You'll Also Like:

  • 1.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 2.  Creating a Random Color Turtle in Python
  • 3.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 4.  Selenium maximize_window() Function to Maximize Window in Python
  • 5.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame
  • 6.  How to Multiply Two Numbers in Python
  • 7.  Get Current URL with Selenium in Python
  • 8.  Convert List into Tuple Using Python
  • 9.  Find Quotient and Remainder After Division in Python
  • 10.  Using Python to Remove Last Character from String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy