CWE Rule 336
Description
Rule Description
A Pseudo-Random Number Generator (PRNG) uses the same seed each time the product is initialized.
Polyspace Implementation
The rule checker checks for Deterministic random output from constant seed.
Examples
Deterministic random output from constant seed
This issue occurs when you use standard random number generator functions that have deterministic output given a constant seed.
The checker detects this issue with the following random number generator functions:
C Standard Library functions such as
srand
,srandom
andinitstate
OpenSSL functions such as
RAND_seed
andRAND_add
C++ Standard Library functions such as
std::linear_congruential_engine<>::seed()
andstd::mersenne_twister_engine<>::seed()
(and also the constructors of these class templates)
With constant seeds, random number generator functions produce the same output every time your program is run. A hacker can disrupt your program if they know how your program behaves.
Use a different random standard function or use a nonconstant seed.
Some standard random routines are inherently cryptographically weak, and should not be used for security purposes.
#include <stdlib.h> void random_num(void) { srand(12345U); //Noncompliant /* ... */ }
This example initializes a random number generator using srand
with
a constant seed. The random number generation is deterministic,
making this function cryptographically weak.
One possible correction is to use a random number generator
that does not require a seed. This example uses rand_s
.
#define _CRT_RAND_S #include <stdlib.h> #include <stdio.h> unsigned int random_num_time(void) { unsigned int number; errno_t err; err = rand_s(&number); if(err != 0) { return number; } else { return err; } }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)