Easy Tutorial
❮ Python String Isdigit Python Urllib ❯

Python Random Number Generation

Python3 Examples

The following example demonstrates how to generate a random number:

Example

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.tutorialpro.org

# Generate a random number between 0 and 9

# Import the random module
import random

print(random.randint(0,9))

Executing the above code produces the following result:

4

In the above example, we used the randint() function of the random module to generate a random number. Each execution returns a different number (from 0 to 9). The syntax of the function is:

random.randint(a,b)

The function returns a number N, where N is between a and b (a <= N <= b), including a and b.

Python3 Examples

❮ Python String Isdigit Python Urllib ❯