Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. random.random() / random.randint() / random.choice()

random.random() / random.randint() / random.choice()

Functions provided by the random module for generating random numbers, making random selections, and shuffling sequences. Commonly used in games, test data generation, and sampling.

Syntax

import random

# Returns a random floating-point number in the range [0.0, 1.0).
random.random()

# Returns a random integer between a and b (inclusive).
random.randint(a, b)

# Returns a random floating-point number between a and b.
random.uniform(a, b)

# Returns a randomly chosen element from a sequence.
random.choice(sequence)

# Returns a list of k unique elements chosen from the sequence.
random.sample(sequence, k)

# Shuffles the list in place.
random.shuffle(list)

Function List

FunctionDescription
random.random()Returns a random floating-point number in the range [0.0, 1.0).
random.randint(a, b)Returns a random integer N such that a <= N <= b (both endpoints are included).
random.uniform(a, b)Returns a random floating-point number between a and b.
random.choice(sequence)Returns a randomly chosen element from a non-empty sequence (list, tuple, string, etc.).
random.sample(sequence, k)Returns a new list of k unique elements chosen randomly from the sequence. The original sequence is not modified.
random.shuffle(list)Shuffles the elements of a list in place. Returns None.
random.seed(value)Sets the seed for the random number generator. Using the same seed produces the same sequence of random numbers.

Sample Code

import random

# Generate a random float between 0 and 1 using random().
print(random.random())  # e.g., prints '0.37444887175646646'

# Simulate a dice roll using randint().
dice = random.randint(1, 6)
print(f'Dice: {dice}')  # Prints a random value from 1 to 6.

# Generate a random float over a continuous range using uniform().
temp = random.uniform(20.0, 30.0)
print(f'Temperature: {temp:.1f}°C')  # Prints a random value between 20.0 and 30.0.

# Pick a random element from a list using choice().
fruits = ['apple', 'banana', 'cherry', 'date']
picked = random.choice(fruits)
print(f'Picked fruit: {picked}')

# Pick a random character from a string using choice().
letters = 'abcdefghijklmnopqrstuvwxyz'
print(random.choice(letters))

# Pick multiple unique elements using sample().
winners = random.sample(['Alice', 'Bob', 'Carol', 'Dave', 'Eve'], k=3)
print(f'Winners: {winners}')  # Three people are chosen at random.

# Shuffle a list using shuffle().
cards = list(range(1, 14))
random.shuffle(cards)
print(cards)  # Prints the list in a random order.

# Generate a reproducible sequence of random numbers using seed() (useful for testing).
random.seed(42)
print(random.randint(1, 100))  # Always prints the same value when the seed is the same.

Notes

Python's random module uses the Mersenne Twister algorithm to generate high-quality pseudo-random numbers. It is suitable for a wide range of applications such as games, simulations, and test data generation.

random.shuffle() modifies the list in place, so it returns None. If you want to keep the original list unchanged, make a copy before shuffling.

Do not use the random module for security-sensitive purposes. When you need cryptographically secure random values — such as for passwords or tokens — use the secrets module instead (e.g., secrets.token_hex() or secrets.choice()).

If you find any errors or copyright issues, please .