×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Generate random integers between 0 and 9 in Python

In this tutorial, we will learn how to generate random integers between 0 and 9 using different methods in Python? By Sapna Deraje Radhakrishna Last updated : April 14, 2023

Following are the few explanatory illustrations using different python modules, on how to generate random integers? Consider the scenario of generating the random numbers between 0 and 9 (both inclusive).

1. Using randrange()

Syntax

random.randrange(stop)
random.randrange(start, stop, step)

Example

import random

for i in range(10):
    print(random.randrange(10))

Output

0
4
3
2
5
5
3
3
0
9

2. Using randint()

Syntax

random.randint(a,b)

Example

import random

for i in range(10):
    print(random.randint(0,10))

Output

10
7
0
10
6
4
8
7
9
2

3. Using randbelow()

By using this method, we can generate cryptographically strong random numbers.

Syntax

randbelow(a)

Example

from secrets import randbelow

for i in range(10):
    print(randbelow(10))

Output

0
8
4
0
0
2
7
0
6
6

Python Basic Programs »


Related Programs

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.