Random#

Introduction#

The C++ Standard Library provides functions to generate pseudo-random numbers. These can be used in VEXcode V5 for behaviors that require variability or chance.

Member Functions#

The random library includes the following functions:

  • rand — Returns a random integer.

  • srand — Sets the random seed.

rand#

Returns a random integer with no maximum.

Available Functions
int rand();

Parameters

This function does not accept any parameters.

Return Values

Returns a random integer between 0 and 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

Parameter

Type

Description

seed

unsigned int

The starting seed value.

Return Values

This function does not return a value.

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