You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to generate random integers, but they are always the same each time I run the program. For instance, if I have the one-line program (that I called rand.nit)
If you do call srand (which should not be necessary since it is automatically called since PR #916), you should indeed get different values, which do seem to work on my machine
$ ./rand
657
$ ./rand
509
The only difference I can spot is that you're using the interpreter, and well, he might very well be broken.
Though, on my machine there seems to be no such problem:
So, I'm not sure, if I recall correctly, you work on OSX right ? This would surprise me but this could be an environment-related issue.
If someone could test this too on a mac, we'd be sure (ping @Tagachi)
It looks like srand is marked as obsolete on OS X. They recommend to use arc4random instead for rand, but it cannot be seeded and won't produce the same result. Still there is no reason it doesn't work, it must be a bug in the OS, searching for srand broken on OS X yields a lot of results.
@ablondin It's an ugly workaround but you can define locally a method to access arc4random:
Activity
lbajolet commentedon May 25, 2015
Hi,
If you do call srand (which should not be necessary since it is automatically called since PR #916), you should indeed get different values, which do seem to work on my machine
The only difference I can spot is that you're using the interpreter, and well, he might very well be broken.
Though, on my machine there seems to be no such problem:
So, I'm not sure, if I recall correctly, you work on OSX right ? This would surprise me but this could be an environment-related issue.
If someone could test this too on a mac, we'd be sure (ping @Tagachi)
ablondin commentedon May 25, 2015
Yes, I'm on Mac OS 10.10.3 (Yosemite).
But even when I do
nitc rand.nit && ./rand, I still get the same numbers.xymus commentedon May 25, 2015
It looks like
srandis marked as obsolete on OS X. They recommend to usearc4randominstead forrand, but it cannot be seeded and won't produce the same result. Still there is no reason it doesn't work, it must be a bug in the OS, searching forsrand broken on OS Xyields a lot of results.@ablondin It's an ugly workaround but you can define locally a method to access
arc4random:Linking with #1388.
privat commentedon May 25, 2015
Or easier
http://dilbert.com/strip/2001-10-25
[-]How does `rand` work?[/-][+]`rand` broken on osx[/+]Merge: Own rand seed