<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 13, 2018, at 5:20 AM, Letanyan Arumugam &lt;<a href="mailto:letanyan.a@gmail.com" class="">letanyan.a@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 13 Jan 2018, at 02:24, Jonathan Hull &lt;<a href="mailto:jhull@gbis.com" class="">jhull@gbis.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I think we have different definitions of consistency. &nbsp;I am fine with the ergonomics of (0…100).random() as a convenience, but it really worries me here that everything is special cased. &nbsp;Special cased things are fine for individual projects, but not the standard library. &nbsp;We should make sure that the design is flexible and extensible, and that comes in part from having a consistent interface.</div><div class=""><br class=""></div></div></div></blockquote><div class=""><br class=""></div><div class="">I think we just want different consistencies. Mine is that I want the same mental model of having to get a random value from some explicit ’set’/’space’.</div></div></div></div></blockquote><div><br class=""></div><div>I could get on board with that model pretty easily, but I would like to see a proposal which makes it explicit (and has a path for other things to declare themselves as one of these spaces). &nbsp;Something like:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>protocol RandomSpace {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>func randomElement(using: RandomNumberGenerator)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br class=""></div><div>...and then conform Range to it (as opposed to providing random() only for two special cases of range).</div><div><br class=""></div><div>It would also be interesting to explore the concept of these constrained spaces in general, and how they might be useful in other ways… but that is probably a different proposal.</div><div><br class=""></div><div>Thanks,</div><div>Jon</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Also, as I said before, we really shouldn’t be doing these crazy contortions to avoid ‘random() % 100’. &nbsp;Instead we should look for that pattern and issue with a warning + fixit to change it to random(in:). &nbsp;I think that will be much more effective in actually changing the behavior in the long run.</div><div class=""><br class=""></div><div class="">Finally, tying everything to Range is extremely limiting. &nbsp;I understand if we don’t want to add other types to the standard library, but I should be able to build on what we add to do it myself without having to reinvent the wheel for each type. &nbsp;It is important to have a consistent story for these things (including multi-dimensional types) so that they can interoperate.</div><div class=""><br class=""></div></div></div></blockquote><div class=""><br class=""></div><div class="">As a stated above I don’t think of it as being tied to a range, but rather a set of possible values. If you want to have multi-dimensional generators, could you not add an extension on an array to generate a value treating the array's elements as constraints?</div><div class=""><br class=""></div><div class="">Using CGPoint as an example with Nate’s api design of random.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">enum</span> ConstraintKind&lt;T: <span style="color: #703daa" class="">Comparable</span>&gt; {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #ba2da2" class="">case</span> constant(<span style="color: #4f8187" class="">T</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #ba2da2" class="">case</span> range(<span style="color: #4f8187" class="">T</span>, <span style="color: #4f8187" class="">T</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="color: #ba2da2" class="">case</span><span style="" class=""> custom((</span>RandomNumberGenerator<span style="" class="">) -&gt; </span>T<span style="" class="">)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">enum</span> PointConstraint {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #ba2da2" class="">case</span> x(ConstraintKind&lt;<span style="text-decoration: underline" class="">C</span>GFloat&gt;)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #ba2da2" class="">case</span> y(ConstraintKind&lt;<span style="text-decoration: underline" class="">C</span>GFloat&gt;)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">extension</span> <span style="color: #703daa" class="">Array</span> <span style="color: #ba2da2" class="">where</span> Element == PointConstraint {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #ba2da2" class="">func</span> random(from constraintKind: <span style="color: #703daa" class="">ConstraintKind</span>&lt;<span style="text-decoration: underline ; color: #703daa" class="">C</span><span style="color: #703daa" class="">GFloat</span>&gt;,</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>using generator: </span>RandomNumberGenerator<span style="" class=""> = </span>Random<span style="" class="">.</span><span style="color: #ba2da2" class="">default</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>) -&gt; <span style="color: rgb(112, 61, 170); text-decoration: underline;" class="">C</span><span style="color: rgb(112, 61, 170);" class="">GFloat</span><font color="#4f8187" class=""> {</font></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">switch</span> constraintKind {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .constant(a): <span style="color: #ba2da2" class="">return</span> a</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .range(min, max): <span style="color: #ba2da2" class="">return</span> (min...max).random(using: generator)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .custom(f): <span style="color: #ba2da2" class="">return</span> f(generator)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">func</span>&nbsp;createRandom(using generator: <span style="color: #4f8187" class="">RandomNumberGenerator</span> = <span style="color: #4f8187" class="">Random</span>.<span style="color: #ba2da2" class="">default</span>) -&gt; <span style="text-decoration: underline ; color: #703daa" class="">C</span><span style="color: #703daa" class="">GPoint</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">var</span> x: <span style="text-decoration: underline ; color: #703daa" class="">C</span><span style="color: #703daa" class="">GFloat</span>? = <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">var</span> y: <span style="text-decoration: underline ; color: #703daa" class="">C</span><span style="color: #703daa" class="">GFloat</span>? = <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">for</span> constraint <span style="color: #ba2da2" class="">in</span> <span style="color: #ba2da2" class="">self</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span><span style="color: #ba2da2" class="">switch</span> constraint {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span><span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .<span style="text-decoration: underline" class="">x</span>(c): x = random(from: c, using: generator)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span><span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .<span style="text-decoration: underline" class="">y</span>(c): y = random(from: c, using: generator)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="color: #ba2da2" class="">return</span> CGPoint(x: x ?? <span style="color: #272ad8" class="">0.0</span>, y: y ?? <span style="color: #272ad8" class="">0.0</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> pointSpace: [<span style="color: #4f8187" class="">PointConstraint</span>] = [</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.x(.range(<span style="color: #272ad8" class="">2</span>, <span style="color: #272ad8" class="">32.5</span>)),</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.y(.constant(<span style="color: #272ad8" class="">4</span>))</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">]</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">pointSpace</span>.createRandom()</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><br class=""></div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">This uses the idea that constraints create a space of possible CGPoint values that createRandom 'gets' from.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">You could make array conform to some ConstraintRandom protocol when we get conditional conformance.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">We really should be looking at GamePlayKit more for design inspiration. &nbsp;There are several use-cases there that are being blatantly ignored in this discussion. &nbsp;For example, what if I want to randomly generate a game world (e.g. The square from The Battle For Polytopia” formerly “SuperTribes”)? &nbsp;Or what if I want an effect where it randomly fades in letters from a String. &nbsp;(…).random() will be completely inadequate for these things.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 12, 2018, at 5:11 AM, Letanyan Arumugam &lt;<a href="mailto:letanyan.a@gmail.com" class="">letanyan.a@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Nate’s design follows a consistent idea of getting a random value from some set of values. Adding the static method random() to a type essentially creates an implicit set which you yourself said leads to inconsistency (Double/Int). Secondly I don’t see why random(in:) should be added when it is just a different spelling for what is already provided. If my second statement is incorrect and there’s something I’m missing please correct me?</div><div class=""><br class=""></div><div class="">I think that consistency outweighs the random trapping inconsistency, however I would actually be fine if random returned an optional. Though the way random is used would likely lead to less opportunities for a trap than the other methods you mention.&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Letanyan</div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 12 Jan 2018, at 04:39, Alejandro Alonso &lt;<a href="mailto:aalonso128@outlook.com" class="">aalonso128@outlook.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
<title class=""></title>

<div class="">
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">
If anything, Nate’s design is inconsistent as properties like `.first` and `.last` return an optional, and methods like `.min()` and `.max()` return an optional as well. Having `.random()` on ranges be an exception and return non optionals are inconsistent
 with other collection facilities, and with other collections that aren’t ranges that return optionals on `.random()`.</div>
<div name="messageSignatureSection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">
<br class="">
- Alejandro</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">
<br class="">
On Jan 11, 2018, 12:06 PM -0600, Letanyan Arumugam via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;, wrote:<br class="">
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;" class="">
<div class="">This is really cool and seems very powerful. However I don’t think we should sacrifice consistency for extendability. Especially when the extendability would not be what most people need.&nbsp;</div>
<div class=""><br class="">
</div>
<div class="">What I am basically trying to say is that. I think the proposals current design direction fits better in a Random library rather than the Standard Library. And Nate’s design more directly addresses the motivating points of the proposal.</div>
<div class=""><br class="">
</div>
<div class="">Letanyan</div>
<div class=""><br class="">
</div>
<div class="">
<blockquote type="cite" class="" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<br class="">
<div class="">Sure. Small disclaimer that this was originally written back in the Swift 1~2 days, so it is overdue for a simplifying rewrite.</div>
<div class=""><br class="">
</div>
<div class="">Also, I should point out that the term “Source” has a special meaning in my code. &nbsp;It basically means that something will provide an ~infinite collection of values of a type T. &nbsp; I have what I call a “ConstantSource” which just wraps a T and gives
 it back when asked. &nbsp;But then I have a bunch of other “sources" which let you create repeating patterns and do deferred calculations and things like that. &nbsp;Finally I have a “RandomSource” which is part of what started this discussion. &nbsp;You set up a RandomSource
 with a set of constraints, and then it gives you random values of T that adhere to those constraints (e.g. colors with a range of hues but the same saturation) whenever you ask for them.</div>
<div class=""><br class="">
</div>
<div class="">This is really useful for doing things like graphic effects because, for example, I can ask for a source of colors and a source of line widths and then get out a large variety of interesting patterns from the same algorithm. &nbsp;I can make simple
 stripes with ConstantSources, or I can make repeating patterns of lines with repeating sources, or I can have random colors which look good together by using a RandomSource. &nbsp;I can take a BezierPath and make it look hand-drawn by breaking it into a bunch of
 lines and then offset the points a small amount using a RandomSource of CGVectors.</div>
<div class=""><br class="">
</div>
<div class="">
<div class="">Not sure how useful this concept of randomness (and pattern) is to others, but I find it immensely useful! &nbsp;Not sure of the best way to implement it. &nbsp;The way I do it is a type erased protocol with private conforming structs and then public initializers
 on the type-erasing box. &nbsp;The end result is that I can just say:</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space: pre;"></span>let myConst = Source(1) //ConstantSource with 1 as a value</div>
<div class=""><span class="Apple-tab-span" style="white-space: pre;"></span>let myPattern = Source([1, 2]) //OrderedSource which repeats 1, then 2 over and over forever</div>
<div class=""><span class="Apple-tab-span" style="white-space: pre;"></span>let myMeta = Source([myConst, myPattern]) //Will alternate between sub-sources in order. Can be nested.</div>
<div class=""><span class="Apple-tab-span" style="white-space: pre;"></span>//…and so on.</div>
<div class=""><br class="">
</div>
<div class="">It is quite extensible and can make very complex/interesting patterns very easily. &nbsp;What I like about it is that (well controlled) random values and patterns or constant values can be interchanged very easily.</div>
</div>
<div class=""><br class="">
</div>
<div class="">The RandomSource has a RandomSourceCreatable Protocol that lets it take random bits and turn them into objects/structs of T adhering to the given constraints. &nbsp;This is way more complex under the hood than it needs to be, but it works well in practice,
 and I haven’t gotten around to cleaning it up yet:</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">public</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">protocol</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
RandomSourceCreatable {</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">associatedtype</span> ConstraintType =
<span style="color: #0433ff" class="">Self</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 128, 0); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span>///This should be implimented by simple types without internal components</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span><span style="color: #0433ff" class="">static</span> <span style="" class="">
</span><span style="color: #0433ff" class="">func</span> <span style="" class="">
createRandom(rnd value:</span>RandomSourceValue<span style="" class="">, constraint:</span>RandomSourceConstraint<span style="" class="">&lt;</span>ConstraintType<span style="" class="">&gt;)-&gt;</span>Self</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 128, 0); background-color: rgb(255, 255, 255);" class="">
<span style="" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>///This should be implimented by complex types with multiple axis of constraints</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span><span style="color: #0433ff" class="">static</span> <span style="" class="">
</span><span style="color: #0433ff" class="">func</span> <span style="" class="">
createRandom(rnd value:</span>RandomSourceValue<span style="" class="">, constraints:[</span>String<span style="" class="">:</span>RandomSourceConstraint<span style="" class="">&lt;</span>ConstraintType<span style="" class="">&gt;])-&gt;</span>Self</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 128, 0); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span>///Returns the proper dimension for the type given the constraints</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span><span style="color: #0433ff" class="">static</span> <span style="" class="">
</span><span style="color: #0433ff" class="">func</span> <span style="" class="">
dimension(given contraints:[</span>String<span style="" class="">:</span>RandomSourceConstraint<span style="" class="">&lt;</span>ConstraintType<span style="" class="">&gt;])-&gt;</span>RandomSourceDimension</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 128, 0); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span>///Validates the given contraints to make sure they can create valid objects. Only needs to be overridden for extremely complex types</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">static</span>
<span style="color: #0433ff" class="">func</span> validateConstraints(<span style="color: #0433ff" class="">_</span> constraints:[<span style="color: #3495af" class="">String</span>:<span style="color: #3495af" class="">RandomSourceConstraint</span>&lt;<span style="color: #3495af" class="">ConstraintType</span>&gt;])-&gt;<span style="color: #3495af" class="">Bool</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 128, 0); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span>///Convienience method which provides whitelist of keys for implicit validation of constraints</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">static</span>
<span style="color: #0433ff" class="">var</span> allowedConstraintKeys:<span style="color: #3495af" class="">Set</span>&lt;<span style="color: #3495af" class="">String</span>&gt; {<span style="color: #0433ff" class="">get</span>}</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp;}</div>
<div class=""><br class="">
</div>
<div class="">Most of these things also have default implementations so you only really have to deal with them for complex cases like colors or points. &nbsp;The constraints are given using a dictionary with string keys and a RandomSourceConstraint value, which
 is defined like this:</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">public</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">enum</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
RandomSourceConstraint&lt;T&gt; {</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">case</span> none</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">case</span> constant(<span style="color: #3495af" class="">T</span>)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">case</span> min(<span style="color: #3495af" class="">T</span>)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">case</span> max(<span style="color: #3495af" class="">T</span>)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">case</span> range (<span style="color: #3495af" class="">T</span>,<span style="color: #3495af" class="">T</span>)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">case</span> custom ( (<span style="color: #3495af" class="">RandomSourceValue</span>)-&gt;<span style="color: #3495af" class="">T</span> )</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
<span class="Apple-tab-span" style="white-space:pre"></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
<span class="Apple-tab-span" style="white-space:pre"></span>//A bunch of boring convenience code here that transforms values so I don’t always have to switch on the enum in other code that deals with this. I just ask for the bounds or constrained T (Note: T
 here refers to the type for a single axis as opposed to the generated type. e.g. CGFloat for a point)&nbsp;</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; }</div>
<div class=""><br class="">
</div>
<div class="">I have found that this handles pretty much all of the constraints I need, and the custom constraint is useful for anything exotic (e.g. sig-figs). &nbsp;The RandomSource itself has convenience inits when T is Comparable that let you specify a range
 instead of having to create the constraints yourself.</div>
<div class=""><br class="">
</div>
<div class="">I then have conformed many standard types to RandomSourceCreatable so that I can create Sources out of them. &nbsp;Here is CGPoint for reference:</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">extension</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
</span><span style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">CGPoint</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">:</span><span style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">RandomSourceCreatable</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
{</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span><span style="color: #0433ff" class="">public</span> <span style="" class="">
</span><span style="color: #0433ff" class="">static</span> <span style="" class="">
</span><span style="color: #0433ff" class="">func</span> <span style="" class="">
dimension(given contraints:[</span>String<span style="" class="">:</span>RandomSourceConstraint<span style="" class="">&lt;</span>CGFloat<span style="" class="">&gt;])-&gt;</span>RandomSourceDimension
<span style="" class="">{</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span><span style="color: #0433ff" class="">return</span> <span style="" class="">
</span>RandomSourceDimension<span style="" class="">.</span>manyWord<span style="" class="">(2)</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">public</span>
<span style="color: #0433ff" class="">typealias</span> ConstraintType = <span style="color: #3495af" class="">
CGFloat</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">public</span>
<span style="color: #0433ff" class="">static</span> <span style="color: #0433ff" class="">
var</span> allowedConstraintKeys:<span style="color: #3495af" class="">Set</span>&lt;<span style="color: #3495af" class="">String</span>&gt;{</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">return</span> [<span style="color: #b4261a" class="">"x"</span>,<span style="color: #b4261a" class="">"y"</span>]</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span><span style="color: #0433ff" class="">public</span> <span style="" class="">
</span><span style="color: #0433ff" class="">static</span> <span style="" class="">
</span><span style="color: #0433ff" class="">func</span> <span style="" class="">
createRandom(rnd value:</span>RandomSourceValue<span style="" class="">, constraints:[</span>String<span style="" class="">:</span>RandomSourceConstraint<span style="" class="">&lt;</span>CGFloat<span style="" class="">&gt;])-&gt;</span>CGPoint
<span style="" class="">{</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">let</span> xVal = value.<span style="color: #3495af" class="">value</span>(at: 0)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">let</span> yVal = value.<span style="color: #3495af" class="">value</span>(at: 1)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">
</span></span>//Note: Ints have a better distribution for normal use cases of points</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">let</span> x =
<span style="color: #3495af" class="">CGFloat</span>(<span style="color: #3495af" class="">Int</span>.<span style="color: #3495af" class="">createRandom</span>(rnd: xVal, constraint: constraints[<span style="color: #b4261a" class="">"x"</span>]?.<span style="color: #3495af" class="">asType</span>({<span style="color: #3495af" class="">Int</span>($0
 * 1000)}) ?? .<span style="color: #3495af" class="">none</span>))/1000</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">let</span> y =
<span style="color: #3495af" class="">CGFloat</span>(<span style="color: #3495af" class="">Int</span>.<span style="color: #3495af" class="">createRandom</span>(rnd: yVal, constraint: constraints[<span style="color: #b4261a" class="">"y"</span>]?.<span style="color: #3495af" class="">asType</span>({<span style="color: #3495af" class="">Int</span>($0
 * 1000)}) ?? .<span style="color: #3495af" class="">none</span>))/1000</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span><span style="color: #0433ff" class="">return</span>
<span style="color: #3495af" class="">CGPoint</span>(x: x, y: y)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; }</div>
<div class=""><br class="">
</div>
<div class="">Notice that I have a RandomSourceValue type that provides the random bits of the requested dimension. When I get around to updating this, I might do something closer to the proposal, where I would just pass the generator and grab bits as needed.
 &nbsp;The main reason I did it the way I did is that it lets me have random access to the source very easily. &nbsp;</div>
<div class=""><br class="">
</div>
<div class="">The ‘asType’ method converts a constraint to work with another type (in this case Ints).</div>
<div class=""><br class="">
</div>
<div class="">Colors are a bit more complicated, mainly because I allow a bunch of different constraints, and I also have validation code to make sure the constraints fit together properly. I also ask for different amounts of randomness based on whether it
 is greyscale or contains alpha. Just to give you a sense, here are the allowed constraint keys for a CGColor:</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span></div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">public</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">static</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">var</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
allowedConstraintKeys:</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">Set</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&lt;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">String</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&gt;{</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(180, 38, 26); background-color: rgb(255, 255, 255);" class="">
<span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp;</span> <span style="color: #0433ff" class="">return</span>
<span style="" class="">[</span>"alpha"<span style="" class="">,</span>"gray"<span style="" class="">,</span>"red"<span style="" class="">,</span>"green"<span style="" class="">,</span>"blue"<span style="" class="">,</span> "hue"<span style="" class="">,</span>
 "saturation"<span style="" class="">,</span> "brightness"<span style="" class="">]</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; }</div>
<div class=""><br class="">
</div>
<div class="">and here is the creation method when the keys are for RGBA (I have similar sections for HSBA and greyscale):</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">let</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
rVal = value.</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">value</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(at:
 0)</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> gVal = value.<span style="color: #3495af" class="">value</span>(at: 1)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> bVal = value.<span style="color: #3495af" class="">value</span>(at: 2)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> aVal = value.<span style="color: #3495af" class="">value</span>(at: 3)</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> r = <span style="color: #3495af" class="">
CGFloat</span>.<span style="color: #3495af" class="">createRandom</span>(rnd: rVal, constraint: constraints[<span style="color: #b4261a" class="">"red"</span>] ?? .<span style="color: #3495af" class="">range</span>(0,1))</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> g = <span style="color: #3495af" class="">
CGFloat</span>.<span style="color: #3495af" class="">createRandom</span>(rnd: gVal, constraint: constraints[<span style="color: #b4261a" class="">"green"</span>] ?? .<span style="color: #3495af" class="">range</span>(0,1))</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> b = <span style="color: #3495af" class="">
CGFloat</span>.<span style="color: #3495af" class="">createRandom</span>(rnd: bVal, constraint: constraints[<span style="color: #b4261a" class="">"blue"</span>] ?? .<span style="color: #3495af" class="">range</span>(0,1))</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> a = <span style="color: #3495af" class="">
CGFloat</span>.<span style="color: #3495af" class="">createRandom</span>(rnd: aVal, constraint: constraints[<span style="color: #b4261a" class="">"alpha"</span>] ?? .<span style="color: #3495af" class="">constant</span>(1.0))</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
&nbsp; &nbsp; <span style="color: #0433ff" class="">return</span> <span style="color: #0433ff" class="">
self</span>.<span style="color: #0433ff" class="">init</span>(colorSpace: <span style="color: #3495af" class="">
CGColorSpaceCreateDeviceRGB</span>(), components: [r,g,b,a])!</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">
<br class="">
</div>
<div class=""><br class="">
</div>
<div class="">The end result is that initializing a source of CGColors looks like this (either parameter can be omitted if desired):</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">let</span>
<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">
colorSource:</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">Source</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&lt;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); text-decoration: underline; color: rgb(52, 149, 175);" class="">C</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">GColor</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&gt;
 =&nbsp;</span><span style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">Source</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(seed: optionalSeed,
 constraints:[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(180, 38, 26);" class="">"saturation"</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">:
 .constant(0.4),</span> <span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(180, 38, 26);" class="">
"brightness"</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">: .constant(0.6)])</span></div>
<div class=""><br class="">
</div>
<div class="">Anyway, I hope this was useful/informative. &nbsp;I know the code is a bit messy, but I still find it enormously useful in practice. &nbsp;I plan to clean it up when I find time, simplifying the RandomSourceValue stuff and moving from String Keys to a Struct
 with static functions for the constraints. &nbsp;The new constraints will probably end up looking like this:</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(4, 51, 255);" class="">let</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&nbsp;colorSource:</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">Source</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&lt;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); text-decoration: underline; color: rgb(52, 149, 175);" class="">C</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(52, 149, 175);" class="">GColor</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&gt;
 =&nbsp;</span><span style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">Source</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(seed: optionalSeed,
 constraints:[</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">.saturation(0.4),&nbsp;</span><span style="background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">.brightness(0.4...0.6)])</span></font></span></div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Jon</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
</div>
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote>
</div>
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote>
</div>
</div>

</div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>