Function randint()
offers a quick and easy way to generate random integers in a given range. It is therefore an optimal tool for a multitude of application cases.
What is the function used for? randint()
?
randint()
is a Python function that is part of the module random. Its purpose is to create a random integer within a range. Unlike some functions in other programming languages, randint()
includes the specified end point of the range, meaning that both the starting value and the ending value can be within the range of possible results. The function randint()
is especially useful in applications based on random values.
There are other functions in the random module to generate random numbers in different ways. For example, random.randrange(start, stop[, step])
offers similar functionality to randint()
but allows to specify an optional step for the sequence of numbers. Similarly, random.randint()
only generates integers, while random.uniform(a, b)
returns a random floating-point number in the specified range. Additionally, the numpy package includes other powerful functions for random number generation, including for arrays and complex mathematical operations.
Structure of randint()
The syntax of the function randint()
Python's random module is relatively simple and requires two parameters: the starting value and the end value of the desired interval for the random integer.
import random
random_number = random.randint(start, end)
python
-
import random
: imports the random module which contains the functionrandint()
. This step is necessary to be able to userandint()
. -
random_number
: the variable in which the result of the function is placedrandint()
is saved. This variable contains the generated random integer. -
start
: the starting value of the interval from which the random integer is to be generated. This number is included in the result. -
fin
: the final value of the interval. This number is also included in the result.
Here is a concrete example:
import random
random_number = random.randint(1, 100)
print(nombre_aléatoire)
python
This code generates a random integer between 1 and 100 and displays it on the console. start
And end
are included in the given limit values, so the generated number can be between 1 and 100.
Use and manipulation of the function randint()
Random generation of the function randint()
can be used flexibly in your program. However, you must ensure that your code is clean and correct to avoid certain error messages.
Multiple calls to randint()
If you call the function randint()
multiple times, you can create multiple random integers in different parts of the code. Each call to randint()
generates a new random integer according to the specified parameters.
import random
nombre_aleatoire1 = random.randint(1, 10)
nombre_aleatoire2 = random.randint(1, 10)
nombre_aleatoire3 = random.randint(1, 10)
# Afficher les entiers aléatoires générés
print("Entier aléatoire 1:", nombre_aleatoire1)
print("Entier aléatoire 2:", nombre_aleatoire2)
print("Entier aléatoire 3:", nombre_aleatoire3)
python
In the above code we use three calls randint()
separated to generate three different random integers between 1 and 10. Each variable random_numberX
stores the result of a call randint()
distinct, and these values can be reused or displayed, depending on the needs of the application.
ValueError for randint()
A ValueError occurs with randint()
if the arguments do not match expectations, for example if the starting value is greater than the ending value or if non-integer arguments are passed. It is therefore advisable to ensure that the parameters are correct to avoid ValueError exceptions and for the function to run smoothly.
# Utilisation correcte de randint()
nombre_aleatoire = random.randint(1, 10)
print("Entier aléatoire entre 1 et 10:", nombre_aleatoire)
# ValueError potentiel si ‘end’ est inférieur à ‘start’
nombre_aleatoire_erreur = random.randint(10, 5) # Cela déclenchera une ValueError
print("Ceci ne sera pas imprimé car une erreur se produit.")
python
Here we first use random.randint(1, 10)
to create a random integer between 1 and 10. Then we try to call random.randint(10, 5)
which will cause a ValueError, because the starting value is larger than the ending value.
TypeError for randint()
A TypeError means that an operation or function is applied to a data type that is not suitable for it. In the context of the function randint()
from the random module, a TypeError may be raised if the passed arguments do not have the expected data type.
import random
random_number_error = random.randint("1", "10")
python
In this example we want to call randint()
with Python strings as arguments. These contain "1"
And "10"
instead of integers. This causes a TypeError, because randint()
expects integers as arguments to define the range of random intervals.
Web hosting
Flexible, high-performance and secure web hosting
- SSL Certificate and DDoS Protection
- Data Backup and Restore
- 24/7 assistance and personal advisor