Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: got rid of all rand() stuff; reviewed

...

It is worth noting that not all random number generators can be seeded. True random number generators that rely on hardware to produce completely unpredictable results do not need to be and cannot be seeded. Some high-quality PRNGs, such as the /dev/random device on some UNIX systems, also cannot be seeded. This rule applies only to algorithmic pseudorandom generators that can be seeded.

MSC30-C. Do not use the rand() function for generating pseudorandom numbers addresses PRNGs from a different perspective, which is the cycle of the pseudorandom number sequence—that is, during a single run of a PRNG, the time interval after which the PRNG generates the same random numbers. MSC30-C disallows use of the rand() function because it generates numbers that have a comparatively short cycle. The same rule proposes the use of the random() function for POSIX and the CryptGenRandom() function for Windows.

...

Noncompliant

...

Code Example

...

This noncompliant code example generates a sequence of 10 pseudorandom numbers using the rand() function. When rand() is not seeded, it uses 1 as a default seed. No matter how many times this code is executed, it always produces the same sequence.

...

bgColor#FFCCCC
langc

...

(

...

The output is as follows:

Code Block
1st run: 41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464,
2nd run: 41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464,
...
nth run: 41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464,

Noncompliant Code Example

Use srand() before rand() to seed the random sequence generated by rand(). The code produces different random number sequences at different calls.

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void func(void) {
  srand(time(NULL)); /* Create seed based on current time */
  for (unsigned int i = 0; i < 10; ++i) {
    /* Generates different sequences at different runs */
    printf("%d, ", rand());
  }
}

The output is as follows:

Code Block
1st run: 25121, 15571, 29839, 2454, 6844, 10186, 27534, 6693, 12456, 5756,
2nd run: 25134, 25796, 2992, 403, 15334, 25893, 7216, 27752, 12966, 13931,
3rd run: 25503, 27950, 22795, 32582, 1233, 10862, 31243, 24650, 11000, 7328,

Although the rand() function is now properly seeded, this solution is still noncompliant because the numbers generated by rand() have a comparatively short cycle, and the numbers can be predictable. (See MSC30-C. Do not use the rand() function for generating pseudorandom numbers.)

Noncompliant Code Example (POSIX)

This noncompliant code example generates a sequence of 10 pseudorandom numbers using the random() function. When random() is not seeded, it behaves like rand(), producing the same sequence of random numbers at different calls.

...

In the previous examples, seeding in rand() and random() is done performed using the time() function, which returns the current time calculated as the number of seconds that have passed since January 1, 1970. Depending on the application and the desirable level of security, a programmer may choose alternative ways to seed PRNGs. In general, hardware is more capable than humans of generating real random numbers (for example, by generating a sequence of bits by sampling the thermal noise of a diode and using the result as a seed).

...