beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

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

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

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap