BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Generate a Random Number

By Chaitanya Singh | Filed Under: Python Examples

In this tutorial, you will learn how to generate a random number in Python.

We will use randint() function of random module to generate a random number between a given range. To use this function, we must import random module in our python program.

Syntax of randint() function

random.randint(x,y)

This will generate a random number in the range of x to y, x and y are inclusive in the range, which means the generated random number lies in the range x <= random number <= y

Program to Generate a Random Number in Python

In this program we are generating a random number in the range of 0 to 100 (0 & 100 are inclusive in the range). We are importing the random module so that we can use the randint() function, which is defined in the random module.

# Program to generate a random number between 0 and 100

# importing the random module
import random

print('Generated Random Number:',random.randint(0,100))

Output:

Python Program to Generate a random number

Related examples:

  • Java program to generate random number – Example
  • SQL Select Random Rows from Table
  • Python Program to Print Calendar
  • Python Program to add Digits of a number

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap