Aléatoire#

Introduction#

La fonction Random donne accès au module random de Python, disponible par défaut dans VEXcode AIM. 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.

Vous trouverez ci-dessous la liste de toutes les méthodes :

  • 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.

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

Générateurs de nombres#

randint#

randint renvoie un entier aléatoire compris entre deux valeurs (incluses), où les deux valeurs sont des entiers.

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.

# Generate a random integer between
# 1 and 10 (inclusive)
random_int = random.randint(1, 10)
robot.screen.print(random_int)

# random_int = (random integer between 1 and 10)

uniform#

uniform renvoie un nombre flottant aléatoire compris entre deux valeurs, où les deux valeurs sont des nombres flottants ou des entiers.

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)
robot.screen.print(random_uniform)

# random_uniform = (random float between 5.0 and 10.0)

randrange#

randrange renvoie un entier aléatoire dans une plage avec un pas optionnel, où les valeurs de début et de fin sont toutes deux des entiers.

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)
robot.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)
robot.screen.print(random_range)

# random_range = (random integer from 5 to 14)

# Generate a random integer from
# 10 to 50, stepping by 5
random_range = random.randrange(10, 50, 5)
robot.screen.print(random_range)

# random_range = (random multiple of 5 between 10 and 45)

random#

random renvoie un nombre flottant aléatoire compris entre 0,0 (inclus) et 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()
robot.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, où le nombre de bits est un entier compris entre 0 et 32.

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)
robot.screen.print(random_bits)

# random_bits = (random integer between 0 and 255)

Sélection#

choice#

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

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])
robot.screen.print(random_choice)

# random_choice = (randomly chosen number from the list)