[swift-users] Make getting a random number more intuitive

Jens Alfke jens at mooseyard.com
Thu Jan 21 15:24:09 CST 2016


> On Jan 21, 2016, at 1:00 PM, Erica Sadun via swift-users <swift-users at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160121/88afa22a/attachment.html>


More information about the swift-users mailing list