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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3, Sat Oct 4 23:33:46 1997 UTC revision 29, Thu Mar 12 01:05:59 1998 UTC
# Line 1  Line 1 
1  (* rand.sml  (* rand.sml
2   *   *
3   * COPYRIGHT (c) 1991 by AT&T Bell Laboratories.  See COPYRIGHT file for details   * COPYRIGHT (c) 1991 by AT&T Bell Laboratories.  See COPYRIGHT file for details
4     * COPYRIGHT (c) 1998 by AT&T Laboratories.  See COPYRIGHT file for details
5   *   *
6   * Random number generator taken from Paulson, pp. 170-171.   * Random number generator taken from Paulson, pp. 170-171.
7   * Recommended by Stephen K. Park and Keith W. Miller,   * Recommended by Stephen K. Park and Keith W. Miller,
# Line 8  Line 9 
9   * CACM 31 (1988), 1192-1201   * CACM 31 (1988), 1192-1201
10   * Updated to include the new preferred multiplier of 48271   * Updated to include the new preferred multiplier of 48271
11   * CACM 36 (1993), 105-110   * CACM 36 (1993), 105-110
12     * Updated to use on Word31.
13   *   *
14   * Note: The Random structure provides a better generator.   * Note: The Random structure provides a better generator.
15   *)   *)
# Line 15  Line 17 
17  structure Rand : RAND =  structure Rand : RAND =
18    struct    struct
19    
20    (* real number version for systems with 46-bit mantissas *)      type rand = Word31.word
21      val a = 48271.0  and  m = 2147483647.0      type rand' = Int32.int  (* internal representation *)
22    
23      val randMin = 1.0      val a : rand' = 48271
24      val randMax = m - 1.0      val m : rand' = 2147483647  (* 2^31 - 1 *)
25        val m_1 = m - 1
26      fun random seed = let      val q = m div a
27            val t = a*seed      val r = m mod a
28    
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            in            in
44              t - m * real(floor(t/m))              if test > 0 then test else test + m
45            end            end
46    
47        val random = intToExt o random' o chk
48    
49      fun mkRandom seed = let      fun mkRandom seed = let
50            val seed = ref seed            val seed = ref (chk seed)
51            in            in
52              fn () => (seed := random (!seed); !seed)              fn () => (seed := random' (!seed); intToExt (!seed))
53            end            end
54    
55      fun norm r = r / m      val real_m = Real.fromLargeInt (Int32.toLarge m)
56        fun norm s = (Real.fromLargeInt (Word31.toLargeInt s)) / real_m
57    
58      fun range (i,j) =      fun range (i,j) =
59            if j < i            if j < i
60              then LibBase.failure{module="Random",func="range",msg="hi < lo"}              then LibBase.failure{module="Random",func="range",msg="hi < lo"}
61              else if j = i then fn _ => i
62              else let              else let
63                val R = real(j - i + 1)              val R = Int32.fromInt j - Int32.fromInt i
64                in              val cvt = Word31.toIntX o Word31.fromLargeInt o Int32.toLarge
                 fn r => i + floor(R*(r/m))  
               end handle _ => let  
                 val ri = real i  
                 val R = (real j)-ri+1.0  
65                  in                  in
66                    fn r => floor(ri + R*(r/m))                if R = m then Word31.toIntX
67                  else fn s => i + cvt ((extToInt s) mod (R+1))
68                  end                  end
69    
70    end (* Rand *)    end (* Rand *)

Legend:
Removed from v.3  
changed lines
  Added in v.29

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