Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/smlnj-lib/Util/rand.sml
ViewVC logotype

Annotation of /sml/trunk/src/smlnj-lib/Util/rand.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 29 - (view) (download)
Original Path: sml/branches/SMLNJ/src/smlnj-lib/Util/rand.sml

1 : monnier 2 (* rand.sml
2 :     *
3 :     * COPYRIGHT (c) 1991 by AT&T Bell Laboratories. See COPYRIGHT file for details
4 : monnier 29 * COPYRIGHT (c) 1998 by AT&T Laboratories. See COPYRIGHT file for details
5 : monnier 2 *
6 :     * Random number generator taken from Paulson, pp. 170-171.
7 :     * Recommended by Stephen K. Park and Keith W. Miller,
8 :     * Random number generators: good ones are hard to find,
9 :     * CACM 31 (1988), 1192-1201
10 :     * Updated to include the new preferred multiplier of 48271
11 :     * CACM 36 (1993), 105-110
12 : monnier 29 * Updated to use on Word31.
13 : monnier 2 *
14 :     * Note: The Random structure provides a better generator.
15 :     *)
16 :    
17 :     structure Rand : RAND =
18 :     struct
19 :    
20 : monnier 29 type rand = Word31.word
21 :     type rand' = Int32.int (* internal representation *)
22 : monnier 2
23 : monnier 29 val a : rand' = 48271
24 :     val m : rand' = 2147483647 (* 2^31 - 1 *)
25 :     val m_1 = m - 1
26 :     val q = m div a
27 :     val r = m mod a
28 : monnier 2
29 : monnier 29 val extToInt = Int32.fromLarge o Word31.toLargeInt
30 :     val intToExt = Word31.fromLargeInt o Int32.toLarge
31 :    
32 :     val randMin : rand = 0w1
33 :     val randMax : rand = intToExt m_1
34 :    
35 :     fun chk 0w0 = 1
36 :     | chk 0wx7fffffff = m_1
37 :     | chk seed = extToInt seed
38 :    
39 :     fun random' seed = let
40 :     val hi = seed div q
41 :     val lo = seed mod q
42 :     val test = a * lo - r * hi
43 : monnier 2 in
44 : monnier 29 if test > 0 then test else test + m
45 : monnier 2 end
46 :    
47 : monnier 29 val random = intToExt o random' o chk
48 :    
49 : monnier 2 fun mkRandom seed = let
50 : monnier 29 val seed = ref (chk seed)
51 : monnier 2 in
52 : monnier 29 fn () => (seed := random' (!seed); intToExt (!seed))
53 : monnier 2 end
54 :    
55 : monnier 29 val real_m = Real.fromLargeInt (Int32.toLarge m)
56 :     fun norm s = (Real.fromLargeInt (Word31.toLargeInt s)) / real_m
57 : monnier 2
58 :     fun range (i,j) =
59 :     if j < i
60 :     then LibBase.failure{module="Random",func="range",msg="hi < lo"}
61 : monnier 29 else if j = i then fn _ => i
62 :     else let
63 :     val R = Int32.fromInt j - Int32.fromInt i
64 :     val cvt = Word31.toIntX o Word31.fromLargeInt o Int32.toLarge
65 :     in
66 :     if R = m then Word31.toIntX
67 :     else fn s => i + cvt ((extToInt s) mod (R+1))
68 :     end
69 : monnier 2
70 :     end (* Rand *)
71 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0