MeshWorld India LogoMeshWorld.

How to Generate Random Numbers in Python: A Quick Guide

Listen to ArticleAI Speech
~1 min read narration
100%
How to Generate Random Numbers in Python: A Quick Guide

Python provides different functions to generate random numbers, defined in random Standard library.

  • The randint() function:
    • Syntax: random.randint(x, y)
    • It return a random integer between specified range [x, y], inclusive of both x and y; i.e x <= n <= y. Here n is the number returned by randint(x, y) function.
  • The random() function:
    • Syntax: random.random()
    • The random() function doesn’t accept any arguments.
    • It return a random floating point value between [0.0, 1.0) range.

In this article we’ll see how to how to generate random number in Python programming language. Let us understand also with an example.

Example for generating integer number with randint() function

Passing the variable as an argument, and then comparing the result to the int class.

import random

print(random.randint(1, 100))
# 24

Example for generating floating point number with random() function

import random

print(random.random())
# 0.7021509552090657

Note that we may get different output because this program generates random number.

Hope you like this!

Keep helping and happy 😄 coding

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive