# Generate Random Float Numbers in Python

In Python, the random module offers the capability to generate arbitrary floating-point numbers. This module encompasses a spectrum of functions enabling the creation of random numbers adhering to diverse distributions, including but not limited to uniform, **Gaussian (normal),** and exponential distributions. These functionalities empower developers to simulate and work with stochastic data in various applications, contributing to the versatility and accuracy of numerical simulations and statistical analyses.

You can use the following functions to generate **random float numbers:**

- random.random()
- random.uniform()
- np.random.rand()
- np.random.uniform()

## Using random.random()

You can generate a random floating-point number within the interval [0.0, 1.0) by utilizing the random module:

In the above code, import the random module and employ the random function to generate a randomized **floating-point value.** This function produces a random number falling within the interval [0.0, 1.0), implying that it generates values that are equal to or greater than 0.0 while remaining strictly less than 1.0.

## Using random.uniform()

The **random.uniform() function,** nested within the random module in Python, facilitates the generation of a random floating-point number within a designated range. This function adheres to a uniform distribution, ensuring that each value within the specified range holds an equal likelihood of being generated, resulting in an equitable distribution of outcomes.

**Syntax:**

where a is the lower bound of the range and b is the **upper bound** of the range (exclusive). The function generates a random float number x such that a ‹= x ‹ b.

Following an example of how to use **random.uniform()** to generate a random float number between 0 and 10:

In the above code, import the random module and employ the uniform() function to generate a random **floating-point number** within the interval [0, 10). The uniform() function yields a random float that ranges from 0 (inclusive) up to, but not including, 10.

## Difference between random.random() and random.uniform()

The main difference between **random.random() ** and random.uniform() is the range of the random numbers they generate.

The random.random() function produces a random floating-point number encompassed within the interval [0.0, 1.0), whereas **random.uniform(a, b)** generates a random floating-point number within the range [a, b), where 'a' and 'b' are parameters furnished to the function.

Consequently, when the requirement pertains to generating a random floating-point number within a specific numerical span, random.uniform(a, b) emerges as a more adaptable choice due to its capacity to define both the lower and upper boundaries of the range. However, if the intention is solely to generate a random float number within the **[0.0, 1.0) range,** random.random() serves as a simpler and more streamlined option.

Furthermore, it's important to acknowledge that random.random() is more efficient and consumes less memory due to its singular generation of a random float number. Conversely, **random.uniform(a, b)** entails a slightly longer processing time and increased memory utilization, as it necessitates creating and retaining a random float number confined within the specified range during each invocation.

## Random float number between range in Python

**How to generate 10 random float number between 0 and 1**

By employing a loop in conjunction with the random function from the random module, you can efficiently generate a sequence of multiple random **floating-point numbers** falling within the interval of 0 and 1 in Python.

In the above code, import the random module and implement a for loop to generate a set of 10 random floating-point numbers. The random function furnishes a random float within the confines of the interval [0.0, 1.0), and within each iteration of the loop, the generated number is displayed through the print statement.

**How to generate a random float number up to 2 decimal places**

In Python, the generation of a random floating-point number with a designated count of decimal places can be achieved through the utilization of the **random.uniform() function** from the random module, in conjunction with the round() function.

In the above code, the random module is imported, and the uniform function is employed to generate a random floating-point number confined **within the range [0, 1).** Subsequently, the round function is applied to round the aforementioned random float number to two decimal places.

**Generate random float number between 0 to 1 up to 3 decimal places**

To generate a random floating-point number between 0 and 1, inclusive of three decimal places, in Python, the **random.random() function** from the random module can be employed in conjunction with the round function.

In the above code, the random module is imported, and the random() function is utilized to generate a random floating-point number within the interval [0.0, 1.0). Subsequently, the round function is applied to round the generated random float number to three decimal places.

## Generate random float number using a NumPy

The generation of random floating-point numbers can be achieved through the utilization of the numpy module. This module encompasses an array of functions dedicated to generating random numbers, with examples such as **numpy.random.rand(),** which facilitates the production of random floating-point numbers within the interval [0, 1).

In the above code, import the numpy module and use the rand() function to generate a random float number in the range [0, 1).

## Generate a random float number between range using Numpy

The numpy module offers the means to generate random floating-point numbers within user-defined ranges. By utilizing the capabilities of the numpy module, which boasts an array of functions for random number generation, you can employ **numpy.random.uniform()** to generate random floating-point numbers situated within a specified numerical range.

In the above code, the numpy module is imported, and the uniform function is employed to generate a random floating-point number within the range [10, 20). The uniform function yields a random float number that is equal to or greater than 10, while remaining strictly less than 20.

### Conclusion

Random float numbers in Python are essential for tasks involving simulations, statistical analysis, and generating data with uncertain properties. They can be generated using the **built-in random module** or the numpy library, providing flexibility to create random floats within predefined ranges, facilitating accurate modeling of real-world variability and uncertainty in computational applications.

**Related Topics**

- Print the following pattern using python
- Python Program to Check Leap Year
- Remove first n characters from a string | Python
- Check if the first and last number of a list is the same | Python

- Number of occurrences of a substring in a string in Python
- Remove Last Element from List in Python
- How to Use Modulo Operator in Python
- Enumerate() in Python
- Writing to a File with Python's print() Function
- How to read csv file in Python
- Dictionary Comprehension in Python
- How to Convert List to String in Python
- How to convert int to string in Python