Search | Synergex.com | Contact Us | Resource Center Login | Home|
News

Quiz
What is the output of the following program?

  main
  record
        seed1 ,int
        seed2 ,int
        num   ,int
   proc
        open(1,o,"TT:")
        xcall  randm(num, seed1, seed2)
        if (num .lt.  13768) then
          writes(1, "eenie")
        else
          writes(1, "meenie")
   end

a. "eenie" 100% of the time.
b. "meenie" 100% of the time.
c. "eenie" 50% of the time, "meenie" 50% of the time, approximately.
d. "eenie" 6.4% of the time, "meenie" 93.6% of the time, approximately.

Correct answer: (a)

This program uses the system-supplied subroutine RANDM to generate a pseudo-random number.  If the number is less than 13768, it outputs "eenie".  Otherwise, it outputs "meenie".  If the number could be any positive integer value, then (d) would be the correct answer, because only 6.4% of the range 0 to ^x(7FFFFFFF) would qualify.  But, the values returned by RANDM are limited to the range of 0 to 32767, so 50% of them are less than 13768 (c).

That isn't the end of the story, however.  Remember we said that RANDM's values are "pseudo-random".  That "pseudo" figures prominently in this case.  RANDM is actually deterministic, and bases its returned value on the two seeds that are passed in and updated on return.  The sequence of values thereby obtained are only random in the sense that they're difficult to predict and will eventually cover all the possible values with a fairly even distribution.

In this program, we have not initialized the seeds.  Because they are in a record within the main routine, they are both initialized to 0 by default.  This means that the program always produces the same value in the pseudo-random sequence that follows two zero seed values -- which happens to be 1.  Therefore, the correct answer is (a).

If this were a subroutine or function with a stack record instead, then the values of the seeds would become unpredictable.  They would have a high probability of 0, but that wouldn't be at all guaranteed.  That would make this example semi-random, with a heavy leaning towards "eenie".

When initializing seed values, it's a good idea to choose a source of information that's both difficult to predict and changes frequently.

Many people use the time of day, down to the second or millisecond.  Some operating systems provide even better sources of entropy, such as /dev/random on Unix.

More information about News and Events

Contact Synergex to learn more about  News and Events