Aléatoire#

Introduction#

La fonction random donne accès au module random de Python, disponible par défaut dans VEXcode AIR. Ses méthodes permettent de générer des nombres aléatoires, des nombres à virgule flottante et des valeurs à partir de listes ou d’intervalles ; elles sont utiles pour introduire de l’imprévisibilité dans les jeux, les comportements et la prise de décision autonome. Pour commencer à utiliser ces fonctions, ajoutez import random en haut de votre projet.

Vous trouverez ci-dessous une liste des méthodes disponibles :

Générateurs de nombres – Produisent directement des nombres ou des bits aléatoires.

  • randint – Renvoie un entier aléatoire entre deux valeurs (incluses).

  • uniform – Renvoie un nombre flottant aléatoire entre deux valeurs.

  • randrange – Renvoie un entier aléatoire à partir d’une plage avec un pas optionnel.

  • random – Renvoie un nombre flottant aléatoire entre 0,0 (inclus) et 1,0 (exclu).

  • getrandbits – Renvoie un entier avec un nombre spécifié de bits aléatoires.

Sélection – Choisir aléatoirement des éléments dans les séquences.

  • choice – Renvoie un élément aléatoire d’une liste ou séquence non vide.

Contrôle d’état – Gérer ou initialiser l’état du générateur de nombres aléatoires.

  • seed – Définit la graine du générateur de nombres aléatoires pour produire des résultats déterministes.

Générateurs de nombres#

randint#

randint renvoie un entier aléatoire dans la plage a (inclus) à b (inclus).

Usage:
random.randint(a, b)

Paramètre

Description

a

Un entier représentant la borne inférieure inclusive de l’intervalle.

b

Un entier représentant la limite supérieure inclusive de l’intervalle.

# Pick a number from 0 to 100
random_int = random.randint(0, 100)
controller.screen.print(random_int)

#random_int = (random integer between 0-100)

uniform#

uniform renvoie un nombre flottant aléatoire entre start et end.

Usage:
random.uniform(start, end)

Paramètre

Description

start

Un nombre flottant ou un entier représentant la limite inférieure de l’intervalle.

end

Un nombre flottant ou un entier représentant la limite supérieure de l’intervalle.

# # Generate a random float between 5.0 and 10.0
random_uniform = random.uniform(5.0, 10.0)
controller.screen.print(random_uniform)

# random_uniform = (random float between 5.0 and 10.0)

randrange#

randrange renvoie un entier aléatoire de la plage spécifiée.

Usage:
random.randrange(start, stop, step)

Paramètre

Description

start

Facultatif. Un entier représentant la valeur de début inclusive de la plage. La valeur par défaut est 0.

stop

Un entier représentant la valeur finale exclusive de la plage.

step

Facultatif. Un entier représentant la différence entre chaque nombre de l’intervalle. La valeur par défaut est 1.

# Generate a random integer from
# 0 to 9 (exclusive of 10)
random_range = random.randrange(10)
controller.screen.print(random_range)

# random_range = (random integer from 0 to 9)

# Generate a random integer from
# 5 to 15 (exclusive of 15)
random_range = random.randrange(5, 15)
controller.screen.print(random_range)

# random_range = (random integer from 5 to 14)

random#

random renvoie un nombre flottant aléatoire dans la plage 0.0 (inclus) à 1.0 (exclu).

Usage:
random.random()

Paramètre

Description

Cette méthode ne comporte aucun paramètre.

# Generate a random float between 0.0 and 1.0
random_float = random.random()
controller.screen.print(random_float)

# random_float = (random float between 0.0 and 1.0)

getrandbits#

getrandbits renvoie un entier avec un nombre spécifié de bits aléatoires.

Usage:
random.getrandbits(n)

Paramètre

Description

n

Un entier représentant le nombre de bits aléatoires (0 <= n <= 32).

# Generate a random integer with 8 random bits
random_bits = random.getrandbits(8)
controller.screen.print(random_bits)

# random_bits = (random integer between 0 and 255)

Sélection#

choice#

choice choisit et renvoie un élément au hasard dans une séquence.

Usage:
random.choice(sequence)

Paramètre

Description

sequence

Une séquence non vide (liste, tuple ou autre objet indexable) à partir de laquelle un élément aléatoire est sélectionné.

# Choose a random number from a list
random_choice = random.choice([10, 20, 30, 40, 50])
controller.screen.print(random_choice)

# random_choice = (randomly chosen number from the list)

Contrôle de l’État#

seed#

La graine seed initialise le générateur de nombres aléatoires, en définissant son état de manière à ce que des résultats aléatoires puissent être reproduits.

Usage:
random.seed(a=None)

Paramètre

Description

a

Facultatif. Valeur initiale. Peut être un entier (int), un nombre à virgule flottante (float), une chaîne de caractères (str), des octets ou un tableau d’octets. Si la valeur None (par défaut), l’heure système actuelle est utilisée.

random.seed(42)

# Always return the same results
print(random.randint(1, 10))
print(random.random())