Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2636. MISRA. The functions with the...
menu mobile close menu
Additional information
toggle menu Contents

V2636. MISRA. The functions with the 'rand' and 'srand' name of <stdlib.h> should not be used.

Apr 04 2025

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:

  • CWE-676
  • MISRA-C-2012-21.24
  • MISRA-C-2023-21.24