From 69e2131d3a26807428f67dc2f823afd988da1bc7 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 6 Jan 2023 09:35:09 -0800 Subject: [PATCH] Fix integer overflow in the getRandomByte() example in the README (#20) --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 460985b..59c4b60 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,9 @@ You can use this implementation based on `std::rand()`: auto kocherga::getRandomByte() -> std::uint8_t { - return static_cast(std::rand() * std::numeric_limits::max() / RAND_MAX); + const auto product = + static_cast(std::rand()) * static_cast(std::numeric_limits::max()); + return static_cast(product / RAND_MAX); } int main()