SCM Repository
Annotation of /sml/trunk/src/smlnj-lib/Util/OLDrand.sml
Parent Directory
|
Revision Log
Revision 26 - (view) (download)
1 : | monnier | 26 | (* 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 : | * Updated to include the new preferred multiplier of 48271 | ||
10 : | * CACM 36 (1993), 105-110 | ||
11 : | * | ||
12 : | * Note: The Random structure provides a better generator. | ||
13 : | *) | ||
14 : | |||
15 : | structure Rand : RAND = | ||
16 : | struct | ||
17 : | |||
18 : | (* real number version for systems with 46-bit mantissas *) | ||
19 : | val a = 48271.0 and m = 2147483647.0 | ||
20 : | |||
21 : | val randMin = 1.0 | ||
22 : | val randMax = m - 1.0 | ||
23 : | |||
24 : | fun random seed = let | ||
25 : | val t = a*seed | ||
26 : | in | ||
27 : | t - m * real(floor(t/m)) | ||
28 : | end | ||
29 : | |||
30 : | fun mkRandom seed = let | ||
31 : | val seed = ref seed | ||
32 : | in | ||
33 : | fn () => (seed := random (!seed); !seed) | ||
34 : | end | ||
35 : | |||
36 : | fun norm r = r / m | ||
37 : | |||
38 : | fun range (i,j) = | ||
39 : | if j < i | ||
40 : | then LibBase.failure{module="Random",func="range",msg="hi < lo"} | ||
41 : | else let | ||
42 : | val R = real(j - i + 1) | ||
43 : | in | ||
44 : | fn r => i + floor(R*(r/m)) | ||
45 : | end handle _ => let | ||
46 : | val ri = real i | ||
47 : | val R = (real j)-ri+1.0 | ||
48 : | in | ||
49 : | fn r => floor(ri + R*(r/m)) | ||
50 : | end | ||
51 : | |||
52 : | end (* Rand *) | ||
53 : |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |