Random uniform() Method in Python:
The uniform() method generates a random floating-point number between the two input values (both included).
Syntax:
random.uniform(a, b)
Parameters
a: This is Required. A number indicating the lowest possible outcome.
b: This is Required. A number indicating the highest possible outcome.
Examples:
Example1:
Input:
Given first number = 10 Given second number = 20
Output:
The random floating-point number between 10 and 20 = 17.597119991005258 (both 10 and 20 included)
Example2:
Input:
Given first number = 80 Given second number = 90
Output:
The random floating-point number between 80 and 90 = 81.63985884463067 (both 80 and 90 included)
Random uniform() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import random module using the import keyword.
- Give the number as static input and store it in a variable.
- Give the other number as static input and store it in another variable.
- Pass the given two numbers as the arguments to the random.uniform() method that generates a random floating-point number between the given two numbers (both included).
- Store it in another variable.
- Print a random floating-point number between the given two numbers (both included).
- The Exit of the Program.
Below is the implementation:
# Import random module using the import keyword. import random # Give the number as static input and store it in a variable. gvn_fstnum = 10 # Give the other number as static input and store it in another variable. gvn_scndnum = 20 # Pass the given two numbers as the arguments to the random.uniform() method # that generates a random floating-point number between the given two numbers # (both included). # Store it in another variable. rslt = random.uniform(gvn_fstnum, gvn_scndnum) # Print a random floating-point number between the two numbers (both included). print("The random floating-point number between", gvn_fstnum, "and", gvn_scndnum, "=", rslt)
Output:
The random floating-point number between 10 and 20 = 17.597119991005258
Method #2: Using Built-in Functions (User Input)
Approach:
- Import random module using the import keyword.
- Give the number as user input using the int(input()) function and store it in a variable.
- Give the other number as user input using the int(input()) function and store it in another variable.
- Pass the given two numbers as the arguments to the random.uniform() method that generates a random floating-point number between the given two numbers (both included).
- Store it in another variable.
- Print a random floating-point number between the given two numbers (both included).
- The Exit of the Program.
Below is the implementation:
# Import random module using the import keyword. import random # Give the number as user input using the int(input()) function and store it in a variable. gvn_fstnum = int(input("Enter some random number = ")) # Give the other number as user input using the int(input()) function # and store it in another variable. gvn_scndnum = int(input("Enter some random number = ")) # Pass the given two numbers as the arguments to the random.uniform() method # that generates a random floating-point number between the given two numbers # (both included). # Store it in another variable. rslt = random.uniform(gvn_fstnum, gvn_scndnum) # Print a random floating-point number between the two numbers (both included). print("The random floating-point number between", gvn_fstnum, "and", gvn_scndnum, "=", rslt)
Output:
Enter some random number = 80 Enter some random number = 90 The random floating-point number between 80 and 90 = 81.63985884463067