Aleatorio#
Introducción#
La biblioteca estándar de C++ proporciona funciones para generar números pseudoaleatorios. Estas se pueden usar en VEXcode IQ (2.ª generación) para comportamientos que requieren variabilidad o azar.
A continuación se muestra una lista de los métodos disponibles:
Generadores de números#
rand#
rand
returns a random integer with no maximum.
Usage:
rand()
Parámetro |
Descripción |
---|---|
Este método no tiene parámetros. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display a random number
Brain.Screen.print("%d", rand());
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display a random number between 0 and 100
Brain.Screen.print("%d", rand() % 101);
}
To return a random integer within a specific range, use the pattern rand() % (max - min + 1) + min
.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Display a random number between 5 and 10
Brain.Screen.print("%d", rand() % (10 - 5 + 1) + 5);
}
srand#
srand
sets the starting value, seed, used by the pseudo-random number generator in the rand function.
Usage:
srand()
Parámetro |
Descripción |
---|---|
Este método no tiene parámetros. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Use the same seed for random number
srand(1);
Brain.Screen.print("%d", rand() % 100);
}