Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Python Random Module Generate Random Numbers and More - Python Random Module Generate Random Numbers and More

Python Random Module Generate Random Numbers and More

Posted on November 11, 2024December 22, 2024 By Rishabh saini No Comments on Python Random Module Generate Random Numbers and More

Python Random Module

The Python random module is a built-in tool for generating random numbers, making it ideal for creating unpredictability in your programs. Whether you’re simulating dice rolls, choosing a random item from a list, or building a game, the random module has a variety of functions to suit your needs. Here’s an overview of some essential functions and how they can add a dynamic edge to your Python projects.

Python Random Module Generate Random Numbers and More

Python Random Module Generate Random Numbers and More

The random() Function

The random.random() function generates a floating-point number between 0.0 and 1.0. It doesn’t require any arguments, making it a straightforward way to introduce randomness.

# Generating a random float
import random
num = random.random()
print(num)

Output:

0.3232640977876686

The randint() Function

For whole numbers, random.randint() provides a simple way to generate a random integer within a specified range. The beginning and ending of the range (inclusive) are the two parameters that the function takes in.

# Producing an arbitrary integer from 1 to 500

import random
num = random.randint(1, 500)
print(num)

Output:

215

Download New Real Time Projects :-Click here

The randrange() Function

The random.randrange() function is similar to randint(), but it allows for more customization by including a step parameter. This function randomly selects a number from a given range, defined by start, stop, and step arguments. The default values for start and step are 0 and 1, respectively.

# Generating values within a specific range
import random
num1 = random.randrange(1, 10)
num2 = random.randrange(1, 10, 2)
print(num1, num2)

Output:

4
9

The choice() Function

Need a random item from a sequence?Choosing a random element from a non-empty sequence—whether it be a string, list, or tuple—is made simple with random.choice().

# Selecting a random element
import random
random_char = random.choice('Random Module')  # from a string
print(random_char)

random_num = random.choice([23, 54, 765, 23, 45, 45])  # from a list
print(random_num)

random_tuple = random.choice((12, 64, 23, 54, 34))  # from a tuple
print(random_tuple)

Output:

M
765
54

The shuffle() Function

The random.shuffle() function is useful when you want to reorder elements in a list. This function randomly changes the order of items within the list, making it a go-to option for shuffling cards or mixing up game elements.

# Shuffling elements in a list
import random
list1 = [34, 23, 65, 86, 23, 43]
random.shuffle(list1)
print(list1)

Output:

[23, 43, 86, 65, 34, 23]

Rock-Paper-Scissors Game Using random

Let’s bring it all together with a simple Rock-Paper-Scissors game that relies on random.randint() for the computer’s choice. This game demonstrates how randomness enhances interactivity in programs.

# Rock-Paper-Scissors Game
import random

def start_game():
    print("Welcome to Rock-Paper-Scissors!")
    print("1: Rock\n2: Paper\n3: Scissors")
    user_choice = int(input("Select an option (1-3): "))

    machine_choice = random.randint(1, 3)
    choices = ["Rock", "Paper", "Scissors"]
    print(f"Machine chose: {choices[machine_choice - 1]}")

    if user_choice == machine_choice:
        print("It's a tie!")
    elif (user_choice == 1 and machine_choice == 3) or (user_choice == 2 and machine_choice == 1) or (user_choice == 3 and machine_choice == 2):
        print("You won!")
    else:
        print("The machine won.")

    if input("Play again? (yes/no): ").lower() == "yes":
        start_game()

start_game()

Python Random Module Generate Random Numbers and More

Rock-Paper-Scissors Game Using random

https://updategadh.com/category/php-project

Additional random Module Functions

The random module also includes various functions to generate numbers and work with sequences:

  • seed(a=None, version=2): Initializes the random number generator with an optional seed.
  • getstate() and setstate(state): Save and restore the generator’s state.
  • getrandbits(k): Generates an integer with k random bits.
  • uniform(a, b): Returns a random float within the specified range [a, b].
  • normalvariate(mu, sigma): Generates a random float based on the normal distribution.


  1. python random randint
  2. random module in python with example
  3. python random float
  4. python random number
  5. Python Random Module
  6. random.sample python
  7. python random number in range
  8. python random uniform
  9. Python Random Module
  10. random module in python pdf
  11. python compiler

Post Views: 712
Python Tags:how to generate random numbers in python, modules in python, Python, python modules, python programming, python random, python random function, python random module, python random module tutorial, python random numbers, Python Tutorial, random, random module, random module in python, random module in python class 12, random module in python in hindi, random module python, random number in python, random number python, random python

Post navigation

Previous Post: Portfolio Page Using HTML, CSS, and JavaScript
Next Post: Most Popular Python Coding Questions & Answer for 2025

More Related Articles

Encrypt and decrypt AES-256 Encryption Using Python Python
Exploring the nsetools Library in Python: A Guide to Real-Time Stock Data - nsetools Library in Python Exploring the nsetools Library in Python: A Guide to Real-Time Stock Data Python
Join Operations in SQL with Python - Join Operations in SQL with Python Join Operations in SQL with Python Python

Leave a Reply Cancel reply

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

You may also like

  1. Python High-Order Functions: A Comprehensive Guide
  2. Finding the Second Largest Number in Python
  3. Python Constructor: A Guide to Initializing Objects in Python
  4. Weather Information App
  5. Database Operations: UPDATE and DELETE in MySQL Using Python
  6. How to Install Django: Step-by-Step Guide

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,615)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,218)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,872)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme