0% found this document useful (0 votes)
4 views5 pages

Random Library Python

This document provides an introduction to the Python random library, explaining its functions and applications for generating randomness in programming. It covers basic functions such as random.randint, random.choice, and random.shuffle, along with practical examples like a number guessing game. Additionally, it highlights the differences between the random module and the more secure secrets module for cryptographic purposes.

Uploaded by

filali filali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Random Library Python

This document provides an introduction to the Python random library, explaining its functions and applications for generating randomness in programming. It covers basic functions such as random.randint, random.choice, and random.shuffle, along with practical examples like a number guessing game. Additionally, it highlights the differences between the random module and the more secure secrets module for cryptographic purposes.

Uploaded by

filali filali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Python random library

Abdesselam Filali
infos@[Link]
[Link]

Mai 02, 2026

This is a Jupyter notebook converted to LATEXusing the nbconvert tool. It contains explanation,
code snippets and outputs related to the Python random library. For any question or remark
please email me or send a request throught my github.

1 What is the Random Module?


The random module in Python is like a magic toolbox that helps you work with randomness! It
allows you to create random numbers, pick random items from a list, or even shuffle things around.
Think of it like this: If you want to roll a virtual dice, pick a winner from a list of names, or create
a surprise by mixing things up, the random module is there to help! You just tell it what kind of
random thing you need, and it does the job for you.

[33]: # Example usage of the random module in Python.


# This will print a random number between 0 and 10 every
# time you run the code!

import random
# pick a random number between 0 and 10
random_number = [Link](0, 10)
print(random_number)

2 Why Use the random Module?


The random module is super handy whenever you need a little unpredictability in your code. It’s
like having a surprise button! You can use it for:
- Games: Want to roll dice, shuffle cards, or make a character appear in a random spot? The
random module makes it happen! - Choosing Random Items: Need to pick a random winner for
a giveaway or choose a random question from a quiz? This module has you covered. - Simulations:
Sometimes, we want to mimic real-world scenarios where things happen by chance, like flipping a
coin or predicting weather patterns. The random module helps create that “real-life” randomness.
- Generating Random Data: Whether it’s a random password, a number for testing, or even
random colors for an art project, this module does it all.

1
3 Tips & Tricks for the random Module
3.1 [Link]() for Repeatable Results
If you need the same “random” results every time (for testing), use [Link]() . This makes
the random output predictable.

[35]: import random


[Link](1) # Always gives the same result
print([Link](1, 10))

3.2 [Link]() for Decimals


Need a random decimal between two numbers? Use [Link]()!

[40]: import random


print([Link](1, 5)) # Random float between 1 and 5

4.043849779650302

3.3 [Link]() to Pick Multiple Items


Want to pick multiple random items without repeating? Use [Link]().

[43]: import random


names = ['Alice', 'Bob', 'Charlie']
print([Link](names, 2)) # Pick 2 random names

['Bob', 'Alice']

4 Basic Functions of the random Module


4.1 [Link](a, b)
This gives you a random number between a and b (including both). It’s like rolling a dice where
you can choose the range of numbers.

[44]: import random


# Get a random number between 1 and 6
dice_roll = [Link](1, 6)
print(dice_roll)

4.2 [Link] (list)


Want to randomly pick an item from a list? This function does exactly that! It’s great for picking a
random winner or choosing a random option.

2
[25]: import random
names = ['Alice', 'Bob', 'Charlie'] # Randomly pick a name
random_name = [Link](names)
print(random_name)

Bob

4.3 [Link]()
This gives you a random float (decimal number) between 0 and 1. It’s like rolling a super tiny dice
where you always get a number between 0.0 and 1.0.

[45]: import random


# Get a random decimal between 0 and 1
random_float = [Link]( )
print(random_float )

0.43276706790505337

4.4 [Link] (list)


Want to mix things up? This function shuffles the items in a list, changing the order around
randomly. Perfect for shuffling a deck of cards or rearranging a list.

[31]: cards = ['Ace', 'King', 'Queen', 'Jack']


# Shuffle the List
[Link](cards )
print(cards )

['Queen', 'Jack', 'King', 'Ace']

5 Examples
5.1 a simple number guessing game
In the following example we will create a simple number guessing game using the random module.
The computer will pick a random number between 0 and 99, and you will have 5 chances to guess it.
Good luck!

[11]: import random


print("Hi welcome to the game, This is a number guessing game.")
print("You got 5 chances to guess the number. Let's start the game")
random_number = [Link](100)
chances = 5
guess_counter = 0
while guess_counter < chances:
guess_counter = guess_counter + 1
my_guess = int(input('Please Enter your Guess : '))
if my_guess == random_number:

3
print(f'The number is {random_number} and you found it right in␣
,→{guess_counter} attempt(s) !')
break
elif guess_counter >= chances and my_guess != random_number:
print(f'Oops sorry, The number is {random_number} better luck next time.
,→')

elif my_guess > random_number:


print(f'Your guess is: {my_guess}, your guess is higher than the number.
,→')

elif my_guess < random_number:


print(f'Your guess is: {my_guess}, your guess is lower than the number.
,→')

Hi welcome to the game, This is a number guessing game.


You got 5 chances to guess the number. Let's start the game
Your guess is: 50, your guess is higher than the number.
Your guess is: 30, your guess is higher than the number.
Your guess is: 10, your guess is higher than the number.
Your guess is: 5, your guess is higher than the number.
Oops sorry, The number is 0 better luck next time.

5.2 Examples of using random module


[24]: import random
print([Link]()) # Random float: 0.0 <= n < 1.0
print([Link](15)) # Random integer from 0 to 14
print([Link](1, 10)) # Random integer from 1 to 10␣
,→(inclusive)

print([Link](['apple', 'banana', 'cherry'])) # Randomly select an␣


,→element from a list

print([Link](range(100), 10)) # Randomly select 10 unique numbers␣


,→from 0 to 99

print([Link]([1, 2, 3, 4, 5])) # Shuffle a list in place, gives None␣


,→but the list is shuffled

print([Link](1, 10)) # Random float: 1.0 <= n < 10.0


print([Link](0, 1)) # Random float: Normal distribution
print([Link](1, 1)) # Random float: Beta distribution
print([Link](1)) # Random float: Exponential␣
,→distribution

print([Link](1, 1)) # Random float: Gamma distribution


print([Link](1, 1)) # Random float: Log-normal␣
,→distribution

print([Link](1, 1)) # Random float: Normal distribution


print([Link](1, 1)) # Random float: Von Mises distribution
print([Link](1)) # Random float: Pareto distribution
print([Link](1, 1)) # Random float: Weibull distribution

4
print([Link](1, 10, 5)) # Random float: Triangular␣
,→distribution

0.7692399727391376
6
4
cherry
[55, 61, 59, 75, 28, 70, 81, 89, 77, 23]
None
4.7705318386012525
-0.2653805448611613
0.04149230578795307
1.1372208695710295
1.828545302009997
2.7682210991018343
0.5588659463563228
4.975064917311508
1.3169818668376212
1.8044662623157683
5.151399445618591

5.3 random vs secrets for generating random numbers


secrets module is used for cryptographic purposes and is more secure than random module.
random module is used for general purposes and is not suitable for cryptographic use.

[19]: import secrets


secure_random = [Link]()
random_number = secure_random.randrange(100)
print(random_number)

26

You might also like