I have written a simple BASIC interpreter that I'm running on Clang/macOS. To implement RND()
, desiring to use as standard C as possible, I used srand/rand. Near the start of the program (in retrobasic.c) I call srand
(yes, once) with either null or a user-passed value:
if (random_seed > -1)
srand(random_seed);
else
srand(time(NULL));
There is only one place that a number is produced:
result.number = ((double)rand() / (double)RAND_MAX);
The code that's calling this is INT(RND()*25)+1
and I noticed it returns the same starting number every run, but it changes every hour or so. So last night it was always 25, this morning was 2, and now it's 3. I put a printf("%d", rand());
right after the srand
. Sure enough, that gets me:
200829549
200964005
201098461
201232917
201703513
That seems to be pseudo-seconds in the 3rd/4th digits, which explains the behaviour I'm seeing. This added printf
"fixes" it by pumping the sequence and then any subsequent calls to rand
are fine. I would really like to understand/solve this the right way.
Clang complains about the srand(time(NULL))
, saying it takes an unsigned int
and wants me to cast the time_t
to silence the warning. time_t
is implementation dependant, but I suspect it is really a unsigned long
? Perhaps this is an issue with a 32/64 bit conversion going into srand
? I found a few tantalizing posts, like this one, but no ultimate conclusion.
This is not about bad random sequence, the sequence is fine (for my needs). There are threads that are about bad starting numbers, but invariably they end up being improper srand
use. Does anyone have a suggestion here?