[swift-evolution] [Proposal] Random Unification

Chris Lattner clattner at nondot.org
Sun Sep 10 00:31:07 CDT 2017


> On Sep 8, 2017, at 9:52 AM, Alejandro Alonso via swift-evolution <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.

My 2c:

- I’d love to see some random number initializers get added as initializers on the numeric types.  This is a long standing hole and it would be hugely valuable to fill it.
- I’d love to see several of the most common random kinds supported, and I agree it would be nice (but not required IMO) for the default to be cryptographically secure.
- We should avoid the temptation to nuke this mosquito with a heavy handed solution designed to solve all of the world’s problems: For example, the C++ random number stuff is crazily over-general.  The stdlib should aim to solve (e.g.) the top 3 most common cases, and let a more specialized external library solve the fully general problem (e.g. seed management, every distribution imaginable, etc).

In terms of approach, I’d suggest looking at other libraries that are conceptually similar, e.g. the “simple random data” APIs for numpy:
https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.random.html <https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.random.html>

Things like “return a random number from 0 to 1.0, 0 to N, 0 to INTMAX, sample from a normal/gaussian distribution, and maybe one more should be enough.

-Chris


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


More information about the swift-evolution mailing list