<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.3020" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>SRFI 27</DIV>
<DIV>(require (lib "27.ss" "srfi"))<BR>(random-integer integer) -->
integer</DIV>
<DIV>Greetings, Jos Koot</DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=Jean-Paul.ROY@unice.fr
href="mailto:Jean-Paul.ROY@unice.fr">Jean-Paul.ROY@unice.fr</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=plt-scheme@list.cs.brown.edu
href="mailto:plt-scheme@list.cs.brown.edu">plt-scheme@list.cs.brown.edu</A>
</DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Monday, January 08, 2007 11:54
AM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> [plt-scheme] Re: Generating
Random bignums</DIV>
<DIV><BR></DIV>I use for several years the following code (due to Dorai
Sitaram I <BR>think) to generate random bignums and floats
:<BR><BR>(module srandom mzscheme<BR> (provide
srandom)<BR><BR> (define
(current-time)<BR> (expt (current-milliseconds)
2))<BR><BR> (define srandom:initialize-state #f)<BR>
(define srandom:seed-state #f)<BR> (define srandom
#f)<BR><BR> (let ((ij
#f)<BR> (kl
#f)<BR> (u
#f)<BR> (c
#f)<BR> (cd
#f)<BR> (cm
#f)<BR> (i97
#f)<BR> (j97
#f))<BR><BR> (set!
srandom:initialize-state<BR>
(lambda
()<BR>
(set! ij
1802)<BR>
(set! kl
9373)<BR>
(set! u (make-vector 97
0))<BR>
(set! c (/ 362436.0
16777216.0))<BR>
(set! cd (/ 7654321.0
16777216.0))<BR>
(set! cm (/ 16777213.0
16777216.0))<BR>
(set! i97
96)<BR>
(set! j97 32)))<BR><BR>
(srandom:initialize-state)<BR><BR> (set!
srandom:seed-state<BR>
(lambda
z<BR>
(let ((z-len (length
z))<BR>
(tt
(current-time)))<BR>
(set!
ij<BR>
(if (>= z-len 1) (car
z)<BR>
(modulo tt
31329)))<BR>
(set!
kl<BR>
(if (>= z-len 2) (cadr
z)<BR>
(modulo tt
30082)))<BR>
(let ((i (+ (modulo (inexact->exact (floor (/ ij
177)))<BR>
177)
2))<BR>
(j (+ (modulo ij 177)
2))<BR>
(k (+ (modulo (inexact->exact (floor (/ kl
169)))<BR>
178)
1))<BR>
(l (modulo kl
169))<BR>
(ii
0))<BR>
(let loop ((ii
ii))<BR>
(if (< ii
97)<BR>
(let ((s
0.0)<BR>
(t1
0.5)<BR>
(jj
0))<BR>
(let loop ((jj
jj))<BR>
(if (< jj
24)<BR>
(let ((m (modulo (* (modulo (* i j) <BR>179) k)
179)))<BR>
(set! i
j)<BR>
(set! j
k)<BR>
(set! k
m)<BR>
(set! l (modulo (+ (* 53 l) 1)
169))<BR>
(if (not (< (modulo (* l m) 64)
32))<BR>
(set! s (+ s
t1)))<BR>
(set! t1 (* 0.5
t1))<BR>
(loop (+ jj
1)))))<BR>
(vector-set! u ii
s)<BR>
(loop (+ ii 1)))))))))<BR><BR>
(srandom:seed-state)<BR><BR> (let
((srandom-one<BR>
(lambda
()<BR>
(let ((uni (- (vector-ref u
i97)<BR>
(vector-ref u
j97))))<BR>
(if (< uni
0)<BR>
(set! uni (+ uni
1)))<BR>
(vector-set! u i97
uni)<BR>
(set! i97 (- i97
1))<BR>
(if (< i97
0)<BR>
(set! i97
96))<BR>
(set! j97 (- j97
1))<BR>
(if (< j97 0) (set! j97
96))<BR>
(set! c (- c
cd))<BR>
(if (< c 0) (set! c (+ c
cm)))<BR>
(set! uni (- uni
c))<BR>
(if (< uni 0) (set! uni (+ uni
1)))<BR>
uni))))<BR><BR> (set!
srandom<BR>
(lambda
(n)<BR>
;; n >
0<BR>
(let ((r (* n
(srandom-one))))<BR>
(if (and (integer? n) (exact?
n))<BR>
(inexact->exact (floor
r))<BR>
r))))))<BR><BR>
)<BR><BR><BR>_________________________________________________<BR> For
list-related administrative tasks:<BR> <A
href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</A><BR></BLOCKQUOTE></BODY></HTML>