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/todo/bh/random.sml
ViewVC logotype

Annotation of /sml/trunk/benchmarks/todo/bh/random.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (view) (download)

1 : monnier 193 (* Given a seed, mkRandom returns a psuedo-random number generator.
2 :     This generator is a function whose argument is an integer greater than
3 :     the greatest return value desired.
4 :    
5 :     (Linear Congruential,
6 :     after Sedgewick,"Algorithms", Addison-Wesley, 1983, Chapter 3 pp 37-38.)
7 :     *)
8 :    
9 :     structure Random =
10 :     struct
11 :     fun mkRandom seed =
12 :     let val m = 100000000
13 :     val m1 = 10000
14 :     val b = 31415821
15 :     val a = ref seed
16 :     fun mult (p,q) =
17 :     let val p1 = p div m1
18 :     val p0 = p mod m1
19 :     val q1 = q div m1
20 :     val q0 = q mod m1
21 :     in
22 :     (((p0 * q1 + p1 * q0) mod m1) * m1 + p0 * q0) mod m
23 :     end
24 :     in
25 :     (fn r => (a := (mult(!a,b) + 1) mod m;
26 :     ((!a div m1) * r) div m1))
27 :     end
28 :     end

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