'Random' Improvements


(Pedro Ferreira) #1

Hello, not sure if this has been discussed before, but I would like to say
a suggestion of something that could be added to swift's standard library.

Right now if you want to get a random number, you need to do it differently
depending on whether you're on macOS or linux (or do both). In linux you
would use the 'random()' function from 'Glibc', and in macOS you would use
the 'arc4random_uniform()' from 'Darwin'.

Given how common it is to want to get a random number, I feel like it would
be worthwhile to have a function in the standard library that deals with
these differences, so that you can just get a random number and not have to
worry about the different OS.
So basically, you would simply import 'Foundation', and then use a standard
'random()' function.

Even better if you also add some helper functions, like a get random
int/float with range parameters.

For example, a possible implementation could be:

import Foundation

    // so we get different numbers every run
srandom(UInt32(NSDate().timeIntervalSince1970))

func getRandom(_ min: Int, _ max: Int) -> Int {
    let diff = max - min + 1

    #if os(Linux)
        return min + Int(random() % diff)
    #else
        return min + Int(arc4random_uniform(UInt32(diff)))
    #endif
}

print(getRandom(50, 100))

Just an idea, thanks for reading, cheers.


(Peter Dillinger) #2

func getRandom(_ min: Int, _ max: Int) -> Int {

   let diff = max - min + 1

   #if os(Linux)

       return min + Int(random() % diff)

...

Implementation detail: non-uniformity mistakes of the past should not be repeated. http://stackoverflow.com/a/6852396/454544 (Timed out on finding a better reference.)

···

--

Peter Dillinger, Ph.D.

Software Engineering Manager, Coverity Analysis, Software Integrity Group | Synopsys

www.synopsys.com/software


(Pedro Ferreira) #3

Thanks for the correction.
I guess that helps my point, its easy to get this sort of thing wrong, so
it would be a good candidate for being included in the standard library.

···

On Mon, Apr 3, 2017 at 6:53 PM, Peter Dillinger < Peter.Dillinger@synopsys.com> wrote:

> func getRandom(_ min: Int, _ max: Int) -> Int {

> let diff = max - min + 1

>

> #if os(Linux)

> return min + Int(random() % diff)

> ...

Implementation detail: non-uniformity mistakes of the past should not be
repeated. http://stackoverflow.com/a/6852396/454544 (Timed out on
finding a better reference.)

--

Peter Dillinger, Ph.D.

Software Engineering Manager, Coverity Analysis, Software Integrity Group
> Synopsys

www.synopsys.com/software


(David Rönnqvist) #4

I’m just going to mention that there was a tiny discussion about Random in early March (only five messages, all linked to below).

That thread discussed the different expectations/understandings of “random”.

Regards,
David

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033473.html
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033474.html
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033477.html
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033478.html
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033482.html

···

On 3 Apr 2017, at 21:24, Pedro Ferreira via swift-evolution <swift-evolution@swift.org> wrote:

Thanks for the correction.
I guess that helps my point, its easy to get this sort of thing wrong, so it would be a good candidate for being included in the standard library.

On Mon, Apr 3, 2017 at 6:53 PM, Peter Dillinger <Peter.Dillinger@synopsys.com <mailto:Peter.Dillinger@synopsys.com>> wrote:
> func getRandom(_ min: Int, _ max: Int) -> Int {

> let diff = max - min + 1

>

> #if os(Linux)

> return min + Int(random() % diff)

> ...

Implementation detail: non-uniformity mistakes of the past should not be repeated. http://stackoverflow.com/a/6852396/454544 (Timed out on finding a better reference.)

--

Peter Dillinger, Ph.D.

Software Engineering Manager, Coverity Analysis, Software Integrity Group | Synopsys

www.synopsys.com/software <http://www.synopsys.com/software>

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


(Peter Dillinger) #5

Haha:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033482.html

In general, I’d recommend against providing a default that we know won’t meet everyone’s needs,
as randomness can have security properties.

The same could be said of Hashable and even String concatenation, and bad randomness isn't even in the OWAP Top 10 (by my reading).

···

--
Peter Dillinger, Ph.D.
Software Engineering Manager, Coverity Analysis, Software Integrity Group | Synopsys
www.synopsys.com/software


(Charlie Monroe) #6

Haha:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033482.html

In general, I’d recommend against providing a default that we know won’t meet everyone’s needs,
as randomness can have security properties.

The same could be said of Hashable and even String concatenation, and bad randomness isn't even in the OWAP Top 10 (by my reading).

Making some "mistakes" in the past shouldn't encourage to make them in the present.

···

On Apr 5, 2017, at 7:00 PM, Peter Dillinger via swift-evolution <swift-evolution@swift.org> wrote:

--
Peter Dillinger, Ph.D.
Software Engineering Manager, Coverity Analysis, Software Integrity Group | Synopsys
www.synopsys.com/software

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


(Jacob Bandes-Storch) #7

Directly copying a swift-evolution email I sent almost exactly a year ago:

I encourage anyone thinking about PRNG APIs to check out what C++ STL has

to offer: http://en.cppreference.com/w/cpp/numeric/random
And this analysis/extension of it: http://www.pcg-random.org/
posts/ease-of-use-without-loss-of-power.html

I also found Milos Rankovic's response worth considering:
https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160404/001676.html

-Jacob

···

On Wed, Apr 5, 2017 at 10:25 AM, Charlie Monroe via swift-evolution < swift-evolution@swift.org> wrote:

> On Apr 5, 2017, at 7:00 PM, Peter Dillinger via swift-evolution < > swift-evolution@swift.org> wrote:
>
> Haha:
>
>> https://lists.swift.org/pipermail/swift-evolution/Week-of-
Mon-20170227/033482.html
>>
>> In general, I’d recommend against providing a default that we know
won’t meet everyone’s needs,
>> as randomness can have security properties.
>
> The same could be said of Hashable and even String concatenation, and
bad randomness isn't even in the OWAP Top 10 (by my reading).

Making some "mistakes" in the past shouldn't encourage to make them in the
present.

>
> --
> Peter Dillinger, Ph.D.
> Software Engineering Manager, Coverity Analysis, Software Integrity
Group | Synopsys
> www.synopsys.com/software
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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