<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-2022-jp"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">C++11 defines PRNGs in the STL, but even then the API is fairly complex thanks to the need for so many different options for source, distribution, seeds, security... - and it *still* doesn$B!G(Bt cover all of what I consider the obvious cases (YMMV).<div class=""><br class=""></div><div class="">I agree with Robert here, Swift isn$B!G(Bt ready to have that heavy a functionality be part of its core library. For that matter, I question if it$B!G(Bs even a question of readiness so much as one of the API being suitable to a stdlib at all! Having the basic tools available via Darwin/Glibc and/or Foundation is plenty. If you have a need for stronger guarantees in your entropy than you can get from the common functions like rand, random, *rand48, arc4random, etc., it$B!G(Bs unlikely that any stdlib-provided option is going to meet your needs anyway.<div class=""><div class=""><br class=""></div><div class="">I mean, not that I don$B!G(Bt wish there was something like ***SSL$B!G(Bs RAND functionality around, but that just underscores the heck out of how much this is something that belongs in its own library and is too heavy for the stdlib. A coherent, vaguely safe API for randomness is nontrivial, to say the least.</div><div class=""><br class=""></div><div class="">In fact, the strongest argument against it is that it$B!G(Bs just too easy for novices to shoot themselves in the foot with a builtin PRNG. Swift is supposed to make it *harder* to write broken unsafe code, not easier. And that$B!G(Bs not even considering that of the available OS-level functions only random_r() explicitly provides a thread-safety guarantee (so it$B!G(Bs even *easier* for users to mess themselves up without even realizing it, especially with libdispatch being so awesome in Swift 3).</div><div class=""><br class=""></div><div class="">Swift shouldn't promote the use of RNGs by users who don't understand their pitfalls, and adding it to the stdlib does just that. At least forcing users to explicitly call into C APIs, or find existing libraries which have hopefully already focused their time on these considerations, adds an extra step that will ideally lead them to be a bit more conservative - C is "hard mode" from Swift$B!G(Bs POV. (One could argue that it$B!G(Bll instead encourage them to roll their own insecure implementations, but that$B!G(Bll happen with or without a builtin API so why make it easier?)</div><div class=""><div class=""><br class=""><div class="">
<div class="">-- Gwynne Raskind</div><div class="">More magic than a mere signature can contain</div></div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 3, 2016, at 13:27, Robert Widmann via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span class=""></span></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="">Trouble is, most modern languages disagree with that and have chosen to split them out of their standard libraries.</div><div class=""><br class=""></div><div class="">- Rust&nbsp;<a href="https://github.com/rust-lang-nursery/rand" class="">https://github.com/rust-lang-nursery/rand</a></div><div class="">- Go&nbsp;<a href="https://golang.org/pkg/math/rand/" class="">https://golang.org/pkg/math/rand/</a></div><div class="">- Dart&nbsp;<a href="https://api.dartlang.org/stable/1.17.1/dart-math/Random-class.html" class="">https://api.dartlang.org/stable/1.17.1/dart-math/Random-class.html</a></div><div class="">- Elm&nbsp;<a href="https://github.com/jcollard/elm-random" class="">https://github.com/jcollard/elm-random</a></div><div class="">- Haskell&nbsp;<a href="http://hackage.haskell.org/package/random-1.1/docs/System-Random.html#" class="">http://hackage.haskell.org/package/random-1.1/docs/System-Random.html#</a></div><div class=""><br class=""></div><div class="">As far as I can see, recent languages that do have [P]RNGs in their standard libraries are the ones that rely on massive STLs: Clojure, Scala, (java.*), C#, F# (.NET), Python, Ruby, (built-ins. &nbsp;Perhaps by virtue of their implementations in C).<br class=""><br class="">Speaking as someone that has had to write such a control for a library, I don't think it should be included, at least not yet. &nbsp;Not because it's not useful, but because there doesn't seem to be a concrete plan for how it will work here to suit more than one use-case ("gimme a random number"). &nbsp;[P]RNGs are not one-size-fits-all and if the community upholds one particular implementation, it will uphold not just the good but the bad of the invariants it carries with it. &nbsp;Better to let the community offer a wide selection of algorithms and interfaces and let the user pick than let them reach for a tailor-made tool for a broad class of functionality.<br class=""><br class=""><div class="">~Robert Widmann</div></div><div class=""><br class="">2016/07/03 5:37$B!"(BJames Andrews via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; $B$N%a%C%;!<%8(B:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class="">Thanks for the feedback. I agree in that I believe the standard library should be able to grow. I think a PRNG implementation is a sufficiently $B!H(Batomic$B!I(B building block to be included in the standard library for a modern language these days.&nbsp;</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class="">As to the implementation, would it not be foolish to stray to far from the already well proven, &nbsp;existing well understood PRNG implementations?</div><br class=""><div id="bloop_sign_1467548420347300096" class="bloop_sign"><span style="font-family: helvetica, arial; font-size: 13px;" class=""></span>Thanks,<div class="">James</div><br class=""></div><br class=""><p class="airmail_on">On 3 July 2016 at 07:46:16, Austin Zheng (<a href="mailto:austinzheng@gmail.com" class="">austinzheng@gmail.com</a>) wrote:</p><blockquote type="cite" class="clean_bq"><span class=""><div class=""><div class=""></div><div class="">I am also just a random guy, and I agree with much of what Brent says, but my personal opinion is that the standard library should be allowed to grow in the future to encompass areas whose generality and usefulness are comparable to those of current standard library constructs. I would consider a sufficiently well-designed suite of PRNGs to fall into this category.<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">I guess what I'm saying is that, after Swift 3.0 is released, someone (maybe you!) should develop an argument as to why PRNGs belong in the standard library, and pitch it to the swift-evolution list. The worst that can happen is that people (or the core team) disagrees on principle, and at least you have an answer. But if there's interest a proposal can be drafted and taken to the review stage, at which point it'll be considered and possibly accepted.<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">Best,<span class="Apple-converted-space">&nbsp;</span><br class="">Austin<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">&gt; On Jul 2, 2016, at 11:30 PM, Brent Royal-Gordon via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt;&gt; On Jun 24, 2016, at 6:38 AM, James Andrews via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt;&gt; Is there a reason why random() is missing from the standard library? Is<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;&gt; it just a matter of someone implementing it?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; (Note: I'm just a guy, not someone in a leadership position.)<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; Random number generation is surprisingly complicated:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Is it a truly random number generator or a pseudorandom one?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * If it's truly random:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Where are we getting the random data?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Does it require platform-specific code?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Is there I/O involved?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * If it's pseudorandom:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * What's our goal here$B!=(Bcryptography, statistics, both, neither? Cryptographic PRNGs are slow for statistics, but statistical PRNGs break the security of crypto algorithms.<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * What algorithm?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Does it use shared state, or do you initialize your own RNG instance?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * If it's shared:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * How does it behave under concurrency?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Can you change the seed?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * If it's not shared:<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Can you seed it?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * How big is the seed?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Where does the default seed come from?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; * Can you save and restore the state?<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; The Swift standard library is deep in very narrow, fundamental areas: features requiring compiler support, control flow, basic types, fundamental string handling, and sequences and collections. There's just barely enough console I/O and argument support to write a very basic command-line program. There's no file I/O, no concurrency, no networking, not even environment variables. Those things come from Foundation or from other libraries.<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; Random-number generation is another one of those things that Swift leaves to libraries. It is complicated, platform-specific (but not generally architecture-specific), subject to app-specific requirements, implementable in userspace, and does not require compiler support. Ultimately, it simply *does not need to be part of the language* in the way the things in the standard library are. If we wanted to make a protocol for random number generators, we would pretty much just end up with Sequence or Collection, and we already have those sitting in the standard library. The rest is finicky details we can leave to platform creators, library implementors, and users while we work on the things only we can do, like improving our numeric protocols so we can support BigInts and BigFloats.<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; --<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; Brent Royal-Gordon<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; Architechies<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><span class="Apple-converted-space">&nbsp;</span><br class="">&gt; _______________________________________________<span class="Apple-converted-space">&nbsp;</span><br class="">&gt; swift-dev mailing list<span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><span class="Apple-converted-space">&nbsp;</span><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><span class="Apple-converted-space">&nbsp;</span><br class=""><br class=""></div></div></span></blockquote></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-dev mailing list</span><br class=""><span class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a></span><br class=""></div></blockquote></div><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-dev mailing list</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-dev@swift.org" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-dev@swift.org</a><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a></div></blockquote></div><br class=""></div></div></div></div></body></html>