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/benchmarks/programs/b-hut/rand.sml
ViewVC logotype

Annotation of /sml/trunk/benchmarks/programs/b-hut/rand.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (view) (download)

1 : monnier 193 (* rand.sml
2 :     *
3 :     * COPYRIGHT (c) 1991 by AT&T Bell Laboratories. See COPYRIGHT file for details
4 :     *
5 :     * Random number generator taken from Paulson, pp. 170-171.
6 :     * Recommended by Stephen K. Park and Keith W. Miller,
7 :     * Random number generators: good ones are hard to find,
8 :     * CACM 31 (1988), 1192-1201
9 :     *
10 :     * Note: The Random structure provides a better generator.
11 :     *)
12 :    
13 :     structure Rand : RAND =
14 :     struct
15 :    
16 :     (* real number version for systems with 46-bit mantissas *)
17 :     val a = 16807.0 and m = 2147483647.0
18 :    
19 :     val randMin = 1.0
20 :     val randMax = m - 1.0
21 :    
22 :     fun random seed = let
23 :     val t = a*seed
24 :     in
25 :     t - m * real(floor(t/m))
26 :     end
27 :    
28 :     fun mkRandom seed = let
29 :     val seed = ref seed
30 :     in
31 :     fn () => (seed := random (!seed); !seed)
32 :     end
33 :    
34 :     fun norm r = r / m
35 :    
36 :     fun range (i,j) =
37 :     if j < i
38 :     then raise Fail "Rand.range"
39 :     else let
40 :     val R = real(j - i + 1)
41 :     in
42 :     fn r => i + floor(R*(r/m))
43 :     end handle _ => let
44 :     val ri = real i
45 :     val R = (real j)-ri+1.0
46 :     in
47 :     fn r => floor(ri + R*(r/m))
48 :     end
49 :    
50 :     end (* Rand *)
51 :    

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