Random Module

Preview: @BrandonDusch 578 total contributions
BrandonDusch's avatar
BrandonDusch's avatar
@BrandonDusch

578 total contributions

Published Aug 6, 2021Updated Mar 9, 2022
Contribute to Docs

In Python, the built-in random module is used to randomly generate numbers as well as randomly manipulate collections such as lists and strings.

This module can be used when imported at the top of a Python file:

import random
Copy to clipboard
Copy to clipboard

The random variable can then be used for executing the module’s built-in methods, like the .random() method below:

random.random()
Copy to clipboard
Copy to clipboard

Random Module

.choice()
Returns a random item chosen from an iterable argument, such as a list or a dictionary.
.choices()
Returns a list of randomly selected elements from a given sequence, allowing for weighted probabilities and repetition.
.getrandbits()
Generates an integer with a specified number of random bits.
.randint()
Returns a random integer that exists between two values.
.random()
Returns a pseudo-random floating-point number between 0 and 1.
.randrange()
Selects a random number from a defined range of int values.
.sample()
Returns a list of items randomly selected from a given population.
.seed()
Initializes a pseudo-random number generator with a seeded value and sets the first number.
.shuffle()
Takes a list and randomly re-orders the items.
.triangular()
Returns a random floating number from a triangular distribution.
.uniform()
Returns a pseudo-random floating-point number between two given numbers.

All contributors

  1. Preview: @BrandonDusch 578 total contributions
    BrandonDusch's avatar
    BrandonDusch's avatar
    @BrandonDusch

    578 total contributions

  2. Preview: Anonymous contributor 3126 total contributions
    Anonymous contributor's avatar
    Anonymous contributor

    3126 total contributions

  3. Preview: @christian.dinh 2492 total contributions
    christian.dinh's avatar
    christian.dinh's avatar
    @christian.dinh

    2492 total contributions

  1. BrandonDusch's avatar
    BrandonDusch
  2. Anonymous contributor's avatar
    Anonymous contributor
  3. christian.dinh's avatar
    christian.dinh

Contribute to Docs

Learn Python on Codecademy

Morty Proxy This is a proxified and sanitized view of the page, visit original site.