Changeset 13323 for branches/new-random


Ignore:
Timestamp:
Dec 22, 2009, 5:11:10 AM (10 years ago)
Author:
rme
Message:

Remove %next-random-seed, which implemented the old random number
generator. Use new random on all targets.

Correct typo in function %16-random-bits.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/new-random/level-0/l0-numbers.lisp

    r13314 r13323  
    18061806;;; too bad in a 64-bit CCL, but the generator pretty much has to be
    18071807;;; in LAP for 32-bit ports.
    1808 #-x86-target
     1808#-(or x8632-target ppc32-target x8664-target)
    18091809(defun %mrg31k3p (state)
    18101810  (let* ((v state))
     
    18451845  (declaim (inline %16-random-bits)))
    18461846
    1847 #-x86-target
    18481847(defun %16-random-bits (state)
    1849   (%next-random-seed state))
    1850 
    1851 #+x86-target
    1852 (defun %16-random-bits (state)
    1853   (logand #xffff (the fixnum (%mrg31kp3 state))))
     1848  (logand #xffff (the fixnum (%mrg31k3p state))))
    18541849
    18551850#+64-bit-target
     
    18621857    (fast-mod (logior low (the fixnum (ash high 30)))
    18631858              number)))
    1864 
    1865 #|
    1866 Date: Mon, 3 Feb 1997 10:04:08 -0500
    1867 To: info-mcl@digitool.com, wineberg@franz.scs.carleton.ca
    1868 From: dds@flavors.com (Duncan Smith)
    1869 Subject: Re: More info on the random number generator
    1870 Sender: owner-info-mcl@digitool.com
    1871 Precedence: bulk
    1872 
    1873 The generator is a Linear Congruential Generator:
    1874 
    1875    X[n+1] = (aX[n] + c) mod m
    1876 
    1877 where: a = 16807  (Park&Miller recommend 48271)
    1878        c = 0
    1879        m = 2^31 - 1
    1880 
    1881 See: Knuth, Seminumerical Algorithms (Volume 2), Chapter 3.
    1882 
    1883 The period is: 2^31 - 2  (zero is excluded).
    1884 
    1885 What makes this generator so simple is that multiplication and addition mod
    1886 2^n-1 is easy.  See Knuth Ch. 4.3.2 (2nd Ed. p 272).
    1887 
    1888     ab mod m = ...
    1889 
    1890 If         m = 2^n-1
    1891            u = ab mod 2^n
    1892            v = floor( ab / 2^n )
    1893 
    1894     ab mod m = u + v                   :  u+v < 2^n
    1895     ab mod m = ((u + v) mod 2^n) + 1   :  u+v >= 2^n
    1896 
    1897 What we do is use 2b and 2^n so we can do arithemetic mod 2^32 instead of
    1898 2^31.  This reduces the whole generator to 5 instructions on the 680x0 or
    1899 80x86, and 8 on the 60x.
    1900 
    1901 -Duncan
    1902 
    1903 |#
    1904 
    1905 #+(and 32-bit-target (not x86-target))
    1906 (defun %next-random-seed (state)
    1907   (multiple-value-bind (high low) (%next-random-pair (random.seed-1 state)
    1908                                                      (random.seed-2 state))
    1909     (declare (type (unsigned-byte 15) high)
    1910              (type (unsigned-byte 16) low))
    1911     (setf (random.seed-1 state) high
    1912           (random.seed-2 state) low)
    1913     (logior high (thes fixnum (logand low (ash 1 15))))))
    19141859
    19151860;;; When using a dead simple random number generator, it's reasonable
     
    19581903    (* number ratio)))
    19591904
    1960 #+(and 32-bit-target (not x86-target))
    1961 (defun random (number &optional (state *random-state*))
    1962   (if (not (typep state 'random-state)) (report-bad-arg state 'random-state))
    1963   (cond
    1964      ((and (fixnump number) (> (the fixnum number) 0))
    1965       (locally (declare (fixnum number))
    1966         (if (< number 65536)
    1967           (fast-mod (%next-random-seed state) number)
    1968           (let* ((n 0)
    1969                  (nhalf (ash (+ 15 (integer-length number)) -4)))
    1970             (declare (fixnum n nhalf))
    1971             (dotimes (i nhalf (fast-mod n number))
    1972               (setq n (logior (the fixnum (ash n 16))
    1973                               (the fixnum (%next-random-seed state)))))))))
    1974      ((and (typep number 'double-float) (> (the double-float number) 0.0))
    1975       (%float-random number state))
    1976      ((and (typep number 'short-float) (> (the short-float number) 0.0s0))
    1977       (%float-random number state))
    1978      ((and (bignump number) (> number 0))
    1979       (%bignum-random number state))
    1980      (t (report-bad-arg number '(or (integer (0)) (float (0.0)))))))
    1981 
    1982 #+x86-target
    19831905(defun random (number &optional (state *random-state*))
    19841906  (if (not (typep state 'random-state)) (report-bad-arg state 'random-state))
Note: See TracChangeset for help on using the changeset viewer.