Instead of “UnsafeRandomSource”, I would call it “ReproducibleRandomSource”.  I 
have also found that you often need to be able to “rewind” a reproducible 
random source for graphics applications:

        protocol ReproducibleRandomSource : RandomSource {
                init(seed: UInt64)
                func mark()->Int
                func returnToMark(_ mark:Int) //These could use something like 
an index instead of Int. My version just returns 1 the first time you call 
mark(), 2 the second time, etc...
        }

Also, I find that there are just a few primitive types I actually need from a 
random source:

• Double (full coverage of possible values)
• Double (in range of 0…1)
• UInt32

The following are also nice to have (built from the above), and commonly used:

• Bool / CoinFlip
• Int (positive value)
• FixedWidthInteger (of various sizes.  Full coverage of possible values)
• Character/String
• CGFloat

Any other type can be built from these building blocks.  I have a 
RandomSourceCreatable protocol which allows exactly that.  Once you have that, 
you can put a method on random source which allows you to create any conforming 
type:

        extension RandomSource {
                func next<T:RandomSourceCreatable>(_ type: T.Type)->T
        }

This is extremely useful to create colors, offsets, etc…

One thing we need to definitely consider are constraints which people may want 
on the random value.  For example, should an Int be positive?  Should it be in 
a certain range?  I have a “constraints” parameter on my RandomSourceCreatable 
protocol to handle this, and it works well, but I am not 100% happy with the 
ergonomics.  I also have a variant with an “in range:” parameter that works for 
simple linear types like Ints and Floats.  We could do something like:

        extension RandomSource {
                func next<T:RandomRangeCreatable>(_ type: T.Type, in range: 
ClosedRange<T>) -> T 
        }

Finally, don’t underestimate the usefulness of coinFlip and func oneIn(_ 
number:UInt)->Bool.  They let you quickly branch based on a random value:

        if source.oneIn(100) { //This will be true roughly 1 in 100 times
                //Do something occasionally 
        }

Thanks,
Jon


> On Sep 25, 2017, at 9:57 PM, Alejandro Alonso via swift-evolution 
> <[email protected]> wrote:
> 
> Hello evolution,
> 
> I am very thankful for all the feedback, and I’ve been working on a design 
> that tries to utilize everybody’s ideas. The link to what I have so far is 
> here: https://gist.github.com/Azoy/15f0518df38df9b722d4cb17bafea4c1 
> <https://gist.github.com/Azoy/15f0518df38df9b722d4cb17bafea4c1>. Please keep 
> in mind this is just a design, no actual implementation as I would most 
> likely need assistance in doing. The default source for randomness will use 
> the OS’s unseeded CSPRNG. This current setup exposes developers to an API 
> that lets them create their own RandomSources on the fly. I want to make the 
> distinction that any existing or new sources of randomness that conform to 
> UnsafeRandomSource are to be considered non cryptographically secure.
> 
> I would love to get this discussion flowing again, and I would love input on 
> the design. A few things I came across with this design is that some may want 
> to use ranges as an argument for the numeric types, RandomAccessCollection 
> returning an optional when getting a random, and how to incorporate an API 
> that allows distributions. I wanted to get input on how you feel about each 
> of these topics as I’m indifferent about them all. I’m in no way saying this 
> is the design we should go for, but I’m simply providing something I think we 
> should build on or talk about.
> 
> - Alejandro
> 
> On Sep 8, 2017, 11:52 AM -0500, Alejandro Alonso via swift-evolution 
> <[email protected]>, 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
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to