Make getting a random number more intuitive


(David Keck) #1

Is there a plan to make it easier to obtain a random number? Our the current method involving arc4random_uniform isn't very obvious IMO.


(Erica Sadun) #2

* In terms of cross-platform random numbers, there are any number of third party libraries and you can use rand() on many platforms
* In terms of Mac, use GameplayKit for more rational generation. (http://ericasadun.com/2015/06/30/going-random-in-the-age-of-gameplaykit/) You choose the source and distribution.

Not sure this is a Swift question but I hope this helps

-- E

···

On Jan 21, 2016, at 11:19 AM, David Keck via swift-users <swift-users@swift.org> wrote:

Is there a plan to make it easier to obtain a random number? Our the current method involving arc4random_uniform isn't very obvious IMO.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Lukas Stabe) #3

In addition to not being very obvious, arc4random is also not supported on platforms other than OSX.

For a uni project I did recently, I wrote FastRNG[1], which provides a protocol for random number generators, some convenience functions, and a few cross-platform generators.

Be aware that the generators are not able to produce cryptographically secure random, and I’m pretty new to this field. I basically just ported some generators from http://xorshift.di.unimi.it/ to Swift.

I think something like this (the protocol mostly) might be a good fit for future versions of the standard library, but I don’t think it meets the goals stated for Swift 3.

For now, feel free to use FastRNG. I’d also be happy to merge PRs (I think a generator based on arc4random and /dev/random would be pretty cool to have).

— Lukas

[1]: https://github.com/Ahti/FastRNG

···

On 21 Jan 2016, at 19:19, David Keck via swift-users <swift-users@swift.org> wrote:

Is there a plan to make it easier to obtain a random number? Our the current method involving arc4random_uniform isn't very obvious IMO.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Dave Abrahams) #4

There's no plan, but it's something we're very interested in having in
the standard library. The challenge, IMO, is to design something that
produces high-quality results, is intuitive, and is broadly useful. IMO
we probably want to cover the same kind of ground as
http://en.cppreference.com/w/cpp/numeric/random.

···

on Thu Jan 21 2016, David Keck <swift-users-AT-swift.org> wrote:

Is there a plan to make it easier to obtain a random number? Our the
current method involving arc4random_uniform isn't very obvious IMO.

--
-Dave


(Jens Alfke) #5

In addition to not being very obvious, arc4random is also not supported on platforms other than OSX.

It’s also available in OpenBSD and FreeBSD.

Be aware that the generators are not able to produce cryptographically secure random, and I’m pretty new to this field. I basically just ported some generators from http://xorshift.di.unimi.it/ to Swift.

In general it’s better to provide a cryptographically-safe RNG, so people can’t accidentally use an insecure one in a context that needs security. Since every OS provides one of these, I think it’d be best for Swift to provide a single RNG API whose implementation calls the platform’s RNG.

—Jens

···

On Jan 21, 2016, at 12:07 PM, Lukas Stabe via swift-users <swift-users@swift.org> wrote:


(Jens Alfke) #6

rand() is deprecated because it uses a poor algorithm that doesn’t provide enough randomness. From the random(3) man page:

     The random() and srandom() functions have (almost) the same calling
     sequence and initialization properties as the rand(3) and srand(3) func-
     tions. The difference is that rand(3) produces a much less random
     sequence -- in fact, the low dozen bits generated by rand go through a
     cyclic pattern. All of the bits generated by random() are usable. For
     example, `random()&01' will produce a random binary value.

I’d forgotten that random()s sidekick function srandomdev() seeds the RNG with a cryptographic source of entropy, making it usable for secure purposes. So these would be good functions to implement a Swift RNG with.

—Jens

···

On Jan 21, 2016, at 1:00 PM, Erica Sadun via swift-users <swift-users@swift.org> wrote:

* In terms of cross-platform random numbers, there are any number of third party libraries and you can use rand() on many platforms


(Joe Groff) #7

Another important thing arc4random_uniform provides is a uniform distribution between 0 and an arbitrary upper bound. Naively constraining the output of random() by using '%' will give you a biased result if the bound isn't a power of 2.

-Joe

···

On Jan 21, 2016, at 1:24 PM, Jens Alfke via swift-users <swift-users@swift.org> wrote:

On Jan 21, 2016, at 1:00 PM, Erica Sadun via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

* In terms of cross-platform random numbers, there are any number of third party libraries and you can use rand() on many platforms

rand() is deprecated because it uses a poor algorithm that doesn’t provide enough randomness. From the random(3) man page:

     The random() and srandom() functions have (almost) the same calling
     sequence and initialization properties as the rand(3) and srand(3) func-
     tions. The difference is that rand(3) produces a much less random
     sequence -- in fact, the low dozen bits generated by rand go through a
     cyclic pattern. All of the bits generated by random() are usable. For
     example, `random()&01' will produce a random binary value.

I’d forgotten that random()s sidekick function srandomdev() seeds the RNG with a cryptographic source of entropy, making it usable for secure purposes. So these would be good functions to implement a Swift RNG with.


(Erica Sadun) #8

For some reason, when we were trying to test this the other day in irc, srandom()/random() weren't available (dunno why, because googling for linux man pages shows it should be), which is why we fell back to rand/srand.

-- E

···

On Jan 21, 2016, at 2:24 PM, Jens Alfke <jens@mooseyard.com> wrote:

On Jan 21, 2016, at 1:00 PM, Erica Sadun via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

* In terms of cross-platform random numbers, there are any number of third party libraries and you can use rand() on many platforms

rand() is deprecated because it uses a poor algorithm that doesn’t provide enough randomness. From the random(3) man page:

     The random() and srandom() functions have (almost) the same calling
     sequence and initialization properties as the rand(3) and srand(3) func-
     tions. The difference is that rand(3) produces a much less random
     sequence -- in fact, the low dozen bits generated by rand go through a
     cyclic pattern. All of the bits generated by random() are usable. For
     example, `random()&01' will produce a random binary value.

I’d forgotten that random()s sidekick function srandomdev() seeds the RNG with a cryptographic source of entropy, making it usable for secure purposes. So these would be good functions to implement a Swift RNG with.

—Jens