V2636. MISRA. The functions with the 'rand' and 'srand' name of <stdlib.h> should not be used.
This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C.
The rand
and srand
functions from the <stdlib.h>
header file, as well as macros with these names, should not be used.
The srand
and rand
functions are used to work with a pseudorandom number generator. The first function initializes it with a seed value, while the second generates a pseudorandom number.
However, this functionality has a serious drawback: it does not guarantee the quality of the pseudorandom number sequence. So, this functionality from the <stdlib.h>
header file is not recommended for serious tasks that involve pseudorandom numbers.
The code example where the analyzer issues the warning:
int foo()
{
srand(time(NULL));
int random_variable = rand();
}
The analyzer will also issue warnings for using macros with these names:
#define srand printf("msg%i\n", x);
void PositiveTestMacro()
{
int x =42;
srand(x);
}
This diagnostic is classified as:
|