Skip to content

Commit

Permalink
Fix integer overflow in the getRandomByte() example in the README (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko authored Jan 6, 2023
1 parent 0003d46 commit 69e2131
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ You can use this implementation based on `std::rand()`:

auto kocherga::getRandomByte() -> std::uint8_t
{
return static_cast<std::uint8_t>(std::rand() * std::numeric_limits<std::uint8_t>::max() / RAND_MAX);
const auto product =
static_cast<std::uint64_t>(std::rand()) * static_cast<std::uint64_t>(std::numeric_limits<std::uint8_t>::max());
return static_cast<std::uint8_t>(product / RAND_MAX);
}

int main()
Expand Down

0 comments on commit 69e2131

Please sign in to comment.