随机的#
介绍#
C++标准库提供了生成伪随机数的函数。这些函数可用于VEXcode V5中需要变化性或随机性的行为。
Member Functions#
The random library includes the following functions:
rand#
Returns a random integer with no maximum.
Available Functionsint rand();
This function does not accept any parameters.
Return ValuesReturns a random integer between 0 and 32767.
NotesTo 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.
// 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.
void srand( unsigned int seed );
范围 |
Type |
描述 |
|---|---|---|
|
|
初始种子值。 |
This function does not return a value.
Examples// Use the same seed for random number
srand(1);
Brain.Screen.print("%d", rand() % 100);