Bergervei/Zufallszahlen
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>Scheme</language>
== Zufallszahlen und -listen ==
<code id="4d4e5cf0ab781" hidden>
;-- M. Hielscher / Chr. Wagenknecht / V. Berger --
(define random-maker
(let* ((multiplier 48271)
(modulus 2147483647)
(apply-congruence
(lambda (current-seed)
(let ((candidate (modulo (* current-seed multiplier) modulus)))
(if (zero? candidate)
modulus
candidate))))
(coerce
(lambda (proposed-seed)
(if (integer? proposed-seed)
(- modulus (modulo proposed-seed modulus)) 19880214))))
(lambda (initial-seed)
(let ((seed (coerce initial-seed)))
(lambda args
(cond ((null? args)
(set! seed (apply-congruence seed))
(/ (- modulus seed) modulus))
((null? (cdr args))
(let* ((proposed-top
(ceiling (abs (car args))))
(exact-top
(if (inexact? proposed-top)
(inexact->exact proposed-top)
proposed-top))
(top
(if (zero? exact-top)
1
exact-top)))
(set! seed (apply-congruence seed))
(inexact->exact (floor (* top (/ seed modulus))))))
((eq? (cadr args) 'reset)
(set! seed (coerce (car args))))
(else
(display "random: unrecognized message")
(newline))))))))
(require-library 'sisc/libs/srfi/srfi-19)
(import srfi-19)
(define random (random-maker (time-nanosecond (current-time))))
</code>
<code id="4d4dd7fc25664">
;(define RandomSeed 123456789)
(define zufallszahl
(lambda (max)
(+ 1 (randomint max))))
(define zufallsliste
(lambda (max anz)
(if (= anz 0)
()
(cons (zufallszahl max) (zufallsliste max (- anz 1))))))
</code>
<run id="4d4dd7fc256c8">
(zufallsliste 100 20)
</run>