mirror of
https://github.com/espressif/openthread.git
synced 2026-08-02 00:57:47 +00:00
[random] introduce template-based NonCrypto random APIs (#13142)
This commit introduces a new set of template-based APIs for non-cryptographic random number generation in the `Random::NonCrypto` namespace. These new methods provide a cleaner, type-safe, and more robust interface compared to the previous methods. Key additions: - `Generate<UintType>()`: Returns a random value of the given unsigned integer type (`uint8_t`, `uint16_t`, or `uint32_t`). - `GenerateUpToExcluding<UintType>(aMax)`: Returns a random value in the range `[0, aMax)`. - `GenerateFromMinUpToExcluding<UintType>(aMin, aMax)`: Returns a random value in the range `[aMin, aMax)`. - `GenerateInClosedRange<UintType>(aMin, aMax)`: Returns a random value in the closed range `[aMin, aMax]`. The introduction of `GenerateInClosedRange` is an improvement as it safely handles ranges up to the maximum value of the integer type (e.g., `0xffff`) without the risk of overflow. All call sites across the OpenThread core stack and tests have been updated to adopt these new APIs. The public `otRandomNonCrypto` functions are also updated to leverage the new internal methods. Doxygen documentation is added for all new template methods, detailing their behavior, including edge cases where the upper bound is smaller than or equal to the lower bound.
This commit is contained in:
@@ -887,7 +887,7 @@ uint32_t GetRandom(uint32_t max)
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Random::NonCrypto::GetUint32();
|
||||
value = Random::NonCrypto::Generate<uint32_t>();
|
||||
}
|
||||
|
||||
return value % max;
|
||||
|
||||
Reference in New Issue
Block a user