2

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.

Community
  • 1
  • 1
adazacom
  • 443
  • 3
  • 9
  • 1
    The IBM Swift Sandbox is nothing else than an open-source Swift installation on Linux, it behaves exactly the same. I think the tag Linux is what you need – Kametrixom Dec 27 '15 at 11:01
  • Good Point. But if this turns out to be a caching issue or some other "in the browser" issue, then people who have found solutions can find each-other with a specific tag.. – adazacom Dec 29 '15 at 09:19

3 Answers3

2

This was an issue with the way we implemented server-side caching in the Sandbox; non-deterministic code would continually return the same answer even though it should not have. We've disabled it for now, and you should be getting different results with each run. We're currently working on better mechanisms to ensure the scalability of the Sandbox.

I'll see about that tag, as well!

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
  • 1
    Thanks for writing this. I was worried that I was losing my mind. The Sandbox is great. So fast and light weight. I prefer it to Xcode when just messing around. – adazacom Jan 03 '16 at 07:09
  • Glad you like it! A quick and easy programming experience is what we were going for. We're making regular improvements to it as well, so hopefully it keeps getting better as time goes on. – TheSoundDefense Jan 03 '16 at 08:49
1

srand is working as expected. If you change value each time in let time = UInt32(NSDate().timeIntervalSinceReferenceDate) instead of NSDate().timeIntervalSinceReferenceDate with any number, it will output random numbers.

Maybe this is a caching issue, it just doesn't see any changes in code and doesn't send it for recompilation :)

nsinvocation
  • 7,559
  • 3
  • 41
  • 46
0

I don't know what is going on but today it is totally working. So I guess the question is answered:

srand(UInt32(NSDate().timeIntervalSinceReferenceDate))

works fine.

(I think something must have changed. It was behaving the same way (generating the same number with repeated attempts) on two different computers for about 10 days... Bizarre.)

adazacom
  • 443
  • 3
  • 9