ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

From https://www.cplusplus.com/reference/cstdlib/rand/, rand():

Returns a pseudo-random integral number in the range between 0 and RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.

By default, a seed value of 1 is used. Hence, every time you start your program, that seed is used and you will always get the same pseudo random sequence. This can be useful for debugging, but frustrating if you're expecting something that appears random.

A common approach is to seed the random number generator with the current time, e.g.:

srand(time(0));