[swift-evolution] [Proposal] Random Unification

Alejandro Alonso aalonso128 at outlook.com
Mon Sep 11 20:38:28 CDT 2017

I completely agree with you on all points, but I was wondering about the numeric types getting random initializers. I think it’s a great idea, but is this something that should be discussed in a separate proposal? My current goals for the proposal is creating the actual random API, then discussing ideas for additions to RandomAccessCollection+MutableCollection to include .random and .shuffle like Ben mentioned, along with the addition of the numeric initializers.

- Alejandro

On Sep 10, 2017, 12:31 AM -0500, Chris Lattner <clattner at nondot.org>, wrote:

On 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. 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:

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.


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

More information about the swift-evolution mailing list