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.

Categorized in:

Python,

Last Update: February 26, 2024