Random

This is a small improvement, but I still think it is worthwhile.

I would like to see an 'init(randomIn: Range<Self>)’ added to Numeric (where Comparable), which would return a random number in the given range. It is something I have added myself to a subset of number types, but it really should be part of the standard library since there are a million small number types that have to be added individually, there are a couple of tricky edge cases to get right, and my version won’t work with Numeric types added by others.

I would also like to see a ‘randomElement’ method added to collection. This is much easier to get right ourselves, but it comes up often enough in generic code that I still think it is a worthwhile addition…

Thanks,
Jon

While I am not against the idea, I believe that this is not as easy as one would think as everyone has a different understanding of the word "random" as most of random generators aren't "true" random generators.

I think this would ideally require a new protocol RandomGenerator which would be passed to the collection to get the random element, i.e. it would look something like:

arr.randomElement(using: ARC4RandomGenerator())

···

On Mar 4, 2017, at 6:55 AM, Jonathan Hull via swift-evolution <swift-evolution@swift.org> wrote:

This is a small improvement, but I still think it is worthwhile.

I would like to see an 'init(randomIn: Range<Self>)’ added to Numeric (where Comparable), which would return a random number in the given range. It is something I have added myself to a subset of number types, but it really should be part of the standard library since there are a million small number types that have to be added individually, there are a couple of tricky edge cases to get right, and my version won’t work with Numeric types added by others.

I would also like to see a ‘randomElement’ method added to collection. This is much easier to get right ourselves, but it comes up often enough in generic code that I still think it is a worthwhile addition…

Thanks,
Jon
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I suppose the question is whether a default PRNG on numeric types is "good enough", since most developers just need some randomness, it doesn't matter how good it actually is. Anyone with stricter requirements shouldn't be using a black-box, default generator.

That said, my preference is to just keep a stable of useful generators and allow developers to choose their preference; if they want they can easily add a constructor via extension, or a factory method or whatever, in which case they have control over the default.

···

On 4 Mar 2017, at 06:58, Charlie Monroe via swift-evolution <swift-evolution@swift.org> wrote:

While I am not against the idea, I believe that this is not as easy as one would think as everyone has a different understanding of the word "random" as most of random generators aren't "true" random generators.

I think this would ideally require a new protocol RandomGenerator which would be passed to the collection to get the random element, i.e. it would look something like:

arr.randomElement(using: ARC4RandomGenerator())

On Mar 4, 2017, at 6:55 AM, Jonathan Hull via swift-evolution <swift-evolution@swift.org> wrote:

This is a small improvement, but I still think it is worthwhile.

I would like to see an 'init(randomIn: Range<Self>)’ added to Numeric (where Comparable), which would return a random number in the given range. It is something I have added myself to a subset of number types, but it really should be part of the standard library since there are a million small number types that have to be added individually, there are a couple of tricky edge cases to get right, and my version won’t work with Numeric types added by others.

I would also like to see a ‘randomElement’ method added to collection. This is much easier to get right ourselves, but it comes up often enough in generic code that I still think it is a worthwhile addition…

I suppose the question is whether a default PRNG on numeric types is "good enough", since most developers just need some randomness, it doesn't matter how good it actually is. Anyone with stricter requirements shouldn't be using a black-box, default generator.

That said, my preference is to just keep a stable of useful generators and allow developers to choose their preference; if they want they can easily add a constructor via extension, or a factory method or whatever, in which case they have control over the default.

···

On 4 Mar 2017, at 06:58, Charlie Monroe via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

While I am not against the idea, I believe that this is not as easy as one would think as everyone has a different understanding of the word "random" as most of random generators aren't "true" random generators.

I think this would ideally require a new protocol RandomGenerator which would be passed to the collection to get the random element, i.e. it would look something like:

arr.randomElement(using: ARC4RandomGenerator())

On Mar 4, 2017, at 6:55 AM, Jonathan Hull via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

This is a small improvement, but I still think it is worthwhile.

I would like to see an 'init(randomIn: Range<Self>)’ added to Numeric (where Comparable), which would return a random number in the given range. It is something I have added myself to a subset of number types, but it really should be part of the standard library since there are a million small number types that have to be added individually, there are a couple of tricky edge cases to get right, and my version won’t work with Numeric types added by others.

I would also like to see a ‘randomElement’ method added to collection. This is much easier to get right ourselves, but it comes up often enough in generic code that I still think it is a worthwhile addition…

While I am not against the idea, I believe that this is not as easy as one would think as everyone has a different understanding of the word "random" as most of random generators aren't "true" random generators.

I think this would ideally require a new protocol RandomGenerator which would be passed to the collection to get the random element, i.e. it would look something like:

arr.randomElement(using: ARC4RandomGenerator())

Agreed. Probably the best random interface would just allow one to fetch one or more bytes of ‘randomness’, then apply however best toward making a number

I suppose the question is whether a default PRNG on numeric types is "good enough", since most developers just need some randomness, it doesn't matter how good it actually is. Anyone with stricter requirements shouldn't be using a black-box, default generator.

In general, I’d recommend against providing a default that we know won’t meet everyone’s needs, as randomness can have security properties. There are also use cases (like games) where it is important to have multiple identical sources of random data.

A Numeric init(using: RandomSource:, range: Range<Self>) or similar may be more appropriate, but this might be better served as a future extension to be done in conjunction with the server api working group (who I believe are currently working on security/cryptography).

-DW

···

On Mar 4, 2017, at 3:21 AM, Haravikk via swift-evolution <swift-evolution@swift.org> wrote:

On 4 Mar 2017, at 06:58, Charlie Monroe via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote: