home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: ranseq.icn
- #
- # Subject: Procedures to generate all integers over a range, randomly
- #
- # Author: Ralph E. Griswold
- #
- # Date: February 9, 1991
- #
- ###########################################################################
- #
- # This procedure generates the integers from i to j in random order.
- #
- # A linear congruence relationship is used. See Knuth, The Art of
- # Computer Programming, Vol.2, Seminumerical Algorithms, pp. 15-24,
- # 156-157.
- #
- # The constants used here are not selected carefully; the "randomness"
- # of the sequence may not be good.
- #
- ############################################################################
- #
- # Links: nxtprime
- #
- ############################################################################
-
- link nxtprime
-
- procedure ranseq(i, j)
- local x, m, a, c, n
-
- n := j - i + 1
-
- if n < 0 then fail
-
- x := 1
- m := nxtprime(n)
- a := m + 1
- c := nxtprime(m)
-
- every 1 to m do {
- x := (a * x + c) % m
- if x < n then { # discard out-of-range values
- suspend x + i
- }
- }
-
- end
-