[swift-evolution] [Proposal] Random Unification

Stephen Canon scanon at apple.com
Fri Sep 8 13:55:30 CDT 2017


Quick thoughts:

1. A stdlib-level random number facility should default to a host-provided CSPRNG as the generator. Anything less is really not justifiable. The throughput of modern HW-backed CSPRNGs exceeds that of all but the fastest PRNGs, anyway.

2. MT is not a CSPRNG.

– Steve

> On Sep 8, 2017, at 1:31 PM, Kevin Nattinger via swift-evolution <swift-evolution at swift.org> wrote:
> 
> IMO, we should have a `Random` or `RandomGenerator` interface and the stdlib can provide a `SystemRandom` that wraps arc4*/random(), and maybe a cryptographically secure one too (MT19937?).
> 
> protocol RandomGenerator {
>     func uniform() -> Int
>     func uniform() -> Double
>     func uniformBytes(count: Int) -> Data
> }
> 
> Then we could add things like, e.g. a protocol extension to generate normally distributed numbers (https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>).
> 
> extension RandomGenerator {
>     func normal<T: FloatingPoint>() -> (T, T) {
>         // ...
>     }
> }
> 
>> On Sep 8, 2017, at 10:08 AM, Shawn Erickson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> It would be nice to leverage range support instead of a start and end value IMHO.
>> On Fri, Sep 8, 2017 at 9:52 AM Alejandro Alonso via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Hello swift evolution, I would like to propose a unified approach to `random()` in Swift. I have a simple implementation here https://gist.github.com/Azoy/5d294148c8b97d20b96ee64f434bb4f5 <https://gist.github.com/Azoy/5d294148c8b97d20b96ee64f434bb4f5>. This implementation is a simple wrapper over existing random functions so existing code bases will not be affected. Also, this approach introduces a new random feature for Linux users that give them access to upper bounds, as well as a lower bound for both Glibc and Darwin users. This change would be implemented within Foundation.
>> 
>> I believe this simple change could have a very positive impact on new developers learning Swift and experienced developers being able to write single random declarations.
>> 
>> I’d like to hear about your ideas on this proposal, or any implementation changes if need be.
>> 
>> - Alejando
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170908/cf24726a/attachment.html>


More information about the swift-evolution mailing list