#INCLUDE "course.module" --* This sets a seed for the random number generator -- (from the course module) using the current time value. -- The seed must be positive and non-zero and not MAXINT. -- This process sets the seed to a positive number less -- than 2^30, which allows a wide range of different seeds -- to be derived from this one (e.g. an increasing sequence). -- Different seeds are needed for applications involving -- parallel processes with (pseudo-)randomised behaviour. --. --* @param seed : the seed to be initialised. -- PROC random.initialise (RESULT INT seed) TIMER tim: SEQ tim ? seed -- time can be any INT value seed := (seed >> 2) + 42 -- make positive and non-zero : --* If initial random number seeds for different processes -- are close together (e.g. they differ by 1), it takes -- a while for the (relatively simple, but good) random -- number function (from the course module) to generate -- usefully different (pseudo-)random sequences. In such -- cases, it helps to use this routine first, which just -- invokes the random number function a number of times -- (5000 should be enough!). -- --* @param n : the number of calls of the random number function to be made. --* @param seed : an initialised seed (it will be changed!) -- PROC random.warm.up (VAL INT n, INT seed) VAL INT ANY.WILL.DO IS 1024: SEQ i = 0 FOR n INT any: any, seed := random (ANY.WILL.DO, seed) : --* This blocks the invoking process for a random number of -- microseconds up to (but not including) the specified value. -- --* @param max.wait : the maximum wait (in microseconds) --* @param seed : the reandom number seed (it will be changed!) -- PROC random.wait (VAL INT max.wait, INT seed) TIMER tim: INT t, wait: SEQ wait, seed := random (max.wait, seed) tim ? t tim ? AFTER t PLUS wait :