Skip to content

[stdlib] Portable random function #1939

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
Mar 30, 2016

Conversation

tinysun212
Copy link
Contributor

What's in this pull request?

Resolved bug number: N/A


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
OS X platform @swift-ci Please test OS X platform
Linux platform @swift-ci Please test Linux platform

Note: Only members of the Apple organization can trigger swift-ci.

Changed rand32() implementation using a random engine in C++11 <random>.

_swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound) {
if (upper_bound > 0)
upper_bound--;
std::uniform_int_distribution<int> RandomUniform(0, upper_bound);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not std::uniform_int_distribution<__swift_uint32_t>?

@gribozavr
Copy link
Contributor

@tinysun212 LGTM, only minor comments! Let's test this as well:

@swift-ci Please test

@@ -105,7 +105,7 @@ func helperDeleteThree(k1: TestKeyTy, _ k2: TestKeyTy, _ k3: TestKeyTy) {
}

func uniformRandom(max: Int) -> Int {
return Int(arc4random_uniform(UInt32(max)))
return Int(cxx11_mt19937_uniform(UInt32(max)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should be calling rand32() instead.

@gribozavr
Copy link
Contributor

LGTM, let's merge.

@swift-ci Please test and merge

@gribozavr
Copy link
Contributor

@tinysun212 Sorry! CI hit an error:

swift/validation-test/stdlib/Set.swift:108:21: error: missing argument label 'exclusiveUpperBound:' in call
  return Int(rand32(UInt32(max)))
                    ^
                    exclusiveUpperBound: 

Changed rand32() implementation using a random engine in C++11 <random>.
@tinysun212
Copy link
Contributor Author

Fixed and squashed.

@gribozavr
Copy link
Contributor

@swift-ci Please test and merge

@swift-ci swift-ci merged commit 5e6f4cf into swiftlang:master Mar 30, 2016
gribozavr added a commit that referenced this pull request Mar 30, 2016
We were only using arc4random, and now we use C++ <random> instead.  See
also PR #1939.
benrimmington added a commit to benrimmington/swift that referenced this pull request Aug 1, 2018
SwiftPrivate/PRNG.swift:

- currently uses `theGlobalMT19937`;
- previously used `arc4random` (see swiftlang#1939);
- is obsoleted by SE-0202: Random Unification.
@tinysun212 tinysun212 deleted the pr-portable-random branch October 21, 2018 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants