Random Float numbers in Python
In Python, you can generate random floating-point numbers using the random module. The random module provides functions to generate random numbers with different distributions, such as uniform, Gaussian (normal), and exponential. 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 in the range [0.0, 1.0) using the random module:
import random
random_float = random.random()
print(random_float)
//Output:0.9350814328100836
In the above code, import the random module and use the random function to generate a random floating-point number. The random function generates a random number in the range [0.0, 1.0), which means it generates a number greater than or equal to 0.0 and less than 1.0.
Using random.uniform()
The random.uniform() is a function in the random module in Python that returns a random floating-point number in a specified range. The uniform function generates a random number with a uniform distribution, which means that all values in the specified range have an equal probability of being generated. Syntax:
random.uniform(a, b)
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:
import random
random_float = random.uniform(0, 10)
print(random_float)
//Output: 4.263879657538217
In the above code, import the random module and use the uniform function to generate a random float number in the range [0, 10). The uniform function returns a random float number that is greater than or equal to 0 and less than 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() generates a random float number in the range [0.0, 1.0), while random.uniform(a, b) generates a random float number in the range [a, b), where a and b are parameters passed to the function. So, if you need to generate a random float number in a specific range, random.uniform(a, b) is a more flexible option as it allows you to specify the lower and upper bounds of the range. If you only need to generate a random float number in the range [0.0, 1.0), then random.random() is a simpler and more convenient option. Another difference is that random.random() is faster and uses less memory, since it only needs to generate a single random float number. random.uniform(a, b) is slightly slower and uses a bit more memory, as it needs to generate and store a random float numbe r within the specified range each time it is called.
Random float number between range in Python
How to generate 10 random float number between 0 and 1 You can generate multiple random float numbers between 0 and 1 in Python using a loop and the random function from the random module.
import random
for i in range(10):
random_float = random.random()
print(random_float)
//Output:
0.6922449775003727
0.5569364160737686
0.4408478183050522
0.4179307621126126
0.6246593565262336
0.38741237062504444
0.6780574937518249
0.3017040057154086
0.9211191307929942
0.7215239148020885
In the above code, import the random modul e and use a for loop to generate 10 random float numbers. The random function returns a random float number in the range [0.0, 1.0), and we print the generated number in each iteration of the loop.
How to generate a random float number up to 2 decimal places
In Python, you can generate a random float number with a specified number of decimal places using the random.uniform() function from the random module and the round function.
import random
random_float = round(random.uniform(0, 1), 2)
print(random_float)
//Output: 0.73
In the above code, import the random module and use the uniform function to generate a random float number in the range [0, 1). Then, use the round function to round the random float number to 2 decimal places.
Generate random float number between 0 to 1 up to 3 decimal places
You can generate a random float number between 0 and 1 with 3 decimal places in Python using the random.random() function from the random module and the round function.
import random
random_float = round(random.random(), 3)
print(random_float)
//Output: 0.738
In the above code, import the random module and use the random() function to generate a random float number in the range [0.0, 1.0). Then, use the round function to round the random float number to 3 decimal places.
Generate random float number using a NumPy
You can generate a random float number in Python using the numpy module. The numpy module provides a number of functions for generating random numbers, including numpy.random.rand(), which generates random float numbers in range [0, 1).
import numpy as np
random_float = np.random.rand()
print(random_float)
//Output: 0.3525804563459739
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
You can generate a random float number within a specified range in Python using the numpy module. The numpy module provides a number of functions for generating random numbers, including numpy.random.uniform(), which generates random float numbers within a specified range.
import numpy as np
random_float = np.random.uniform(10, 20)
print(random_float)
//Output: 13.153785636272444
In the above code, import the numpy module and use the uniform function to generate a random float number in the range [10, 20). The uniform function returns a random float number that is greater than or equal to 10 and less than 20.
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 using 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