Skip to content

[stdlib] Add a comment about use of Lemire's nearly divisionless method #38263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/public/core/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ extension RandomNumberGenerator {
// unsigned integer will be used, recursing infinitely and probably blowing
// the stack.
@available(*, unavailable)
@_alwaysEmitIntoClient
public mutating func next() -> UInt64 { fatalError() }

/// Returns a value from a uniform, independent distribution of binary data.
Expand Down Expand Up @@ -103,6 +104,9 @@ extension RandomNumberGenerator {
upperBound: T
) -> T {
_precondition(upperBound != 0, "upperBound cannot be zero.")
// We use Lemire's "nearly divisionless" method for generating random
// integers in an interval. For a detailed explanation, see:
// https://arxiv.org/abs/1805.10941
var random: T = next()
var m = random.multipliedFullWidth(by: upperBound)
if m.low < upperBound {
Expand Down