[swift-evolution] [Proposal] Random Unification

Dave DeLong swift at davedelong.com
Thu Nov 30 18:06:26 CST 2017



> On Nov 30, 2017, at 5:02 PM, Jonathan Hull <jhull at gbis.com> wrote:
> 
> 
>> On Nov 30, 2017, at 3:52 PM, Dave DeLong <swift at davedelong.com <mailto:swift at davedelong.com>> wrote:
>> 
>> What is the useful distinction between generating a random value, and choosing a random element from a collection of all possible values?
> 
> I don’t have to generate (or keep in memory) that collection.  
> 
> I gave an example before of an easy to add API for random colors which allow their saturation, lightness, and alpha to be fixed.  I use something very similar all the time in some graphics code I have (varying only the hue or saturation).  I also do the same with Sizes and CGVectors.  How would you represent that as a collection to choose an element from?

You’re not picking a random color. You’re picking a random hue or a random saturation, and then constructing a color based off that. In other words, you’re choosing a value from 0…1:

let color = UIColor(hue: (0…1.0).random(), saturation: knownSaturationValue, brightness: knownBrightnessValue, alpha: knownAlphaValue)

Dave

> Also, for most of my use cases, I *need* to be able to plug in a repeatably random source… otherwise pixels will start jumping around the screen when people resize things.

That’s cool; I’m totally in favor of the RandomSource API as a way to model different distributions or algorithms or whatever. I just don’t thing “Foo.random” is a good idea.

Cheers

Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171130/3e274e34/attachment.html>


More information about the swift-evolution mailing list