Question

I was under the impression, upon starting up Scheme, the randomize procedure was called with the current time as its seed. However, if I have a Scheme script consisting solely of (print (random 10)), the only output I receive is 7; no other number. So, what am I doing wrong? For the record, I am using Chicken Scheme.

Était-ce utile?

La solution

What random library are you using, exactly? according to the documentation your assumption about random's seeding is correct:

(randomize [SEED]) : Set random-number seed. If SEED (an exact integer) is not supplied, the current time is used. On startup (when Unit extras is initialized), the random number generator is initialized with the current time.

(random N) : Returns a pseudo-random integer in [0, N-1]. N is an integer.

Also notice the warnings, in particular the second one that seems to explain the behaviour you're witnessing:

Warning: This procedure uses rand(3) internally and exhibits its deficiencies, including low quality pseudo-randomness:

  • On Windows and Solaris, only 32768 unique random values can be generated in the range [0, N-1]. If N >= 32768, there will be gaps in the result set.
  • On Mac OS X, Windows and some other platforms, little variance in output is seen with nearby seeds. Since the random generator is seeded with current-seconds at startup, new processes may see similar or identical random sequences for up to a minute.
  • On Linux, rand(3) is an alias to random(3), which provides output of reasonable quality.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top