samedi 2 janvier 2016

How to seed rand() in IBM Swift Sandbox?

I am new to StackOverflow so please correct me if there is a better way to post a question which is a specific case of an existing question.

Alberto Barrera answered How does one seed the random number generator in Swift?

with

let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand(time)
print("Random number: \(rand()%10)")

which is perfect generally, but when I try it in The IBM Swift Sandbox it gives the same number sequence every run, at least in the space of a half hour.

import Foundation
import CoreFoundation

let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand(time)
print("Random number: \(rand()%10)")

At the moment, every Run prints 5.

Has anyone found a way to do this in the IBM Sandbox? I have found that random() and srandom() produce a different number sequence but similarly are the same each Run. I haven't found arc4random() in Foundation, CoreFoundation, Darwin, or Glibc.

As an aside, I humbly suggest someone with reputation above 1500 creates a tag IBM-Swift-Sandbox.




Aucun commentaire:

Enregistrer un commentaire