Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Generate Random Number in Python? (Program)
In computer programming, randomness plays a fascinating role, enabling a myriad of applications, from games and simulations to cryptography and data analysis. Python offers robust functionality for the generation of random numbers effortlessly. Here, we will understand how to generate random numbers in Python.
Random number generation adds an element of unpredictability, injecting excitement and novelty into our programs. Python provides a powerful module called "random," which empowers developers to incorporate randomness into their applications with ease.
From generating a single random number to generating sequences of random numbers, the possibilities are endless.
Throughout this post, we provide several examples of how to generate a random number in Python, code snippets, and explanations. Let’s get started!
Python Program to Generate Random Number Between 0 to 100
Code
# How to generate a random number between 0 to 100
import random
# Generate a random number between 1 and 100 (inclusive)
random_number = random.randint(1, 100)
# Display the random number
print("Random number:", random_number)
Output
Random number: 49
(output will be different each time you run the program)
Explanation
In this program, we import the random module, which provides functions for generating random numbers. The randint() function from the random module is used to generate a random integer between the specified range. In this case, we generate a random number between 1 and 100 (inclusive) and store it in the variable random_number.
Finally, we use the print() function to display the random number on the console or output window.
When you run this program, it will generate a random number between 1 and 100 (inclusive) each time it is executed and display the result. The specific number generated will vary each time, adding an element of unpredictability and randomness to the program's output.
Python Program to Generate Random Number Between 0 to 10
Code
# How to generate random number between 0 and 10
import random
# Generate a random number between 0 and 10 (inclusive)
random_number = random.randint(0, 10)
# Display the random number
print("Random number between 0 and 10:", random_number)
Output
Random number between 0 and 10: 5
(output will be different each time you run the program)
Explanation
In this case, we generate a random number between 0 and 10 (inclusive) and store it in the variable random_number.
Next, we use the print() function to display the random number on the console or output window.
Generate Random Number Between 0 to 1
Code
import random
# Generate a random number between 0 and 1
random_number = random.random()
# Display the random number
print("Random number between 0 and 1:", random_number)
Output
Random number between 0 and 10: 0.02015500708918261
(output will be different each time you run the program)
Explanation
The random() function from the random module is used to generate a random float between 0 and 1. The generated number will be greater than or equal to 0 but less than 1.
When you run this program, it will generate a random number between 0 and 1 each time it is executed and display the result.
Generate Random Number With No Repeats
Code
# Python Random Number Generator Without Repeating
import random
# Set the range and number of random numbers to generate
start_range = 1
end_range = 10
num_numbers = 5
# Generate unique random numbers
random_numbers = random.sample(range(start_range, end_range + 1), num_numbers)
# Display the random numbers
print("Random numbers without repeats:", random_numbers)
Output
Random numbers without repeats: [4, 2, 1, 8, 10]
Explanation
- In this program, we import the random module, which provides functions for generating random numbers. We specify the range of numbers to choose from using the variables start_range and end_range.
- We also define the number of random numbers to generate with the variable num_numbers.
- The random.sample() function is then used to generate a list of unique random numbers from the given range. It takes two arguments: the range of numbers (range(start_range, end_range + 1)) and the number of unique random numbers to select (num_numbers).
- Finally, we use the print() function to display the generated random numbers on the console.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program