Aleatorio#

Introducción#

La biblioteca estándar de C++ proporciona funciones para generar números pseudoaleatorios. Estas se pueden usar en VEXcode V5 para comportamientos que requieren variabilidad o azar.

Funciones#

The random library includes the following functions:

  • rand — Returns a random integer.

  • srand — Sets the random seed.

rand#

Devuelve un número entero aleatorio con un máximo de 32.767.

Available Functions
int rand();

Parameters

Esta función no acepta ningún parámetro.

Return Values

Devuelve un número entero aleatorio entre 0 y 32767.

Notes
  • To return a random integer within an exclusive limit, use the pattern rand() % max.

  • To return a random integer within a specific range, use the pattern rand() % (max - min + 1) + min.

Examples
// Display a random number
Brain.Screen.print("%d", rand());

// Display a random number between 0 and 100
Brain.Screen.print("%d", rand() % 101);

// Display a random number between 5 and 10
Brain.Screen.print("%d", rand() % (10 - 5 + 1) + 5);

srand#

Sets the seed to be used by the pseudo-random number generator in the rand function.

Available Functions
void srand( unsigned int seed );

Parameters

Parámetro

Tipo

Descripción

seed

unsigned int

El valor inicial de la semilla.

Return Values

Esta función no devuelve ningún valor.

Examples
// Use the same seed for random number
srand(1);
Brain.Screen.print("%d", rand() % 100);