# Python Program to Generate Random Float

Contents

## Python – Generate Random Float

We can generate a random floating point number in Python using Python random package.

In this tutorial, we shall learn how to generate a random floating point number in the range (0,1) and how to generate a floating point number in between specific minimum and maximum values.

### Syntax of random.random()

Following is the syntax of random() function in random module.

``f = random.random()``

random() function returns a random floating point value in between zero and one.

## Examples

### 1. Random Floating Point Number in the Range (0, 1)

In this example, we shall use random.random() function to generate a random floating point number between 0 and 1.

Python Program

``````import random

# Generate a random floating point number
f = random.random()

print(f)``````
Run Code Copy

Output

``0.6156729963963723``

### 2. Random Floting Point Number in the Range (min, max)

In this example, we shall use random.random() function to generate a random floating point number between a given minimum value and a maximum value.

Python Program

``````import random

# Specific range
min = 2
max = 10

# Generate a random floating point number
f = min + (max-min)*random.random()

print(f)``````
Run Code Copy

min+ ensures that the generated random value has atleast a value of min. (max-min)*random.random() ensures that the value does not exceed the maximum limit.

Explanation

Let us understand the expression used to generate floating point number between a specific range.

``````f = min + (max-min)*random.random()

#random.random generates a value in the range (0, 1)

f = min + (max-min)*(0, 1)

Resulting Range = (min + (max-min)*0, min + (max-min)*1)
= (min + 0, min + max - min)
= (min, max)``````

Output

``4.102801901668189``

## Summary

In this tutorial of Python Examples, we learned how to generate a random floating point number, with the help of well detailed example programs.

## Related Tutorials

Code copied to clipboard successfully 👍