Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

As a solution, you should always ensure that your RNG is properly seeded. Seeding an RNG means that it will generate different sequences of random numbers at any call.

Rule MSC30-C. Do not use the rand() function for generating pseudorandom numbersMSC30-CPP. Do not use the rand() function for generating pseudorandom numbers addresses RNGs from a different perspective, i.e. the time till first collision occurs. In other words, during a single run of an RNG, the time interval after which, the RNG generates the same random numbers. The rule MSC30-C CPP deprecates the rand() function as it generates numbers which have a comparatively short cycle. The same rule proposes the use of random() function for POSIX and CryptGenRandom() function for Windows.

The current rule (MSC32-CCPP) examines these three RNGs in terms of seeding. Noncompliant code examples correspond to the use of an RNG without a seed, while compliant solutions correspond to the same RNG being properly seeded. Rule MSC32-C CPP addresses all three RNGs mentioned in rule MSC30-C CPP for completeness. Rule MSC32-C CPP complies to MSC30-C CPP and does not recommend the use of the rand() function. Nevertheless, if it is unavoidable to use rand(), at least, it should be properly seeded.

...

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.

...

In the previous examples, seeding in rand() and random() is done using the {{time()}} function, which returns the current time calculated as the number of seconds that have past since 01/01/1970. Depending on the application and the desirable level of security, a programmer may choose alternative ways to seed RNGs. In general, hardware is more capable of generating real random numbers (for example generate a sequence of bits by sampling the thermal noise of a diode and use this as a seed).

...

Wiki Markup
[{{CryptGenRandom()}}|http://msdn.microsoft.com/en-us/library/aa379942.aspx]does not run the risk of not being properly seeded. The reason for that is that its arguments serve as seeders. From the Microsoft Developer Network {{CryptGenRandom()}} reference \[[MSDN|https://www.securecoding.cert.org/confluence/display/seccode/AA.+C+References#AA.CReferences-MSDN]\]:

The CryptGenRandom function fills a buffer with cryptographically random bytes.

Syntax

BOOL WINAPI CryptGenRandom(
__in HCRYPTPROV hProv,
__in DWORD dwLen,
__inout BYTE *pbBuffer
);

Parameters

Wiki Markup
hProv \[in\]
    Handle of acryptographic service provider(CSP) created by a call toCryptAcquireContext.
dwLen \[in\]
    Number of bytes of random data to be generated.
pbBuffer \[in, out\]
    Buffer to receive the returned data. This buffer must be at leastdwLenbytes in length.
    Optionally, the application can fill this buffer with data to use as an auxiliary random seed.
\\

...