Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2012-10-02T16:00:16+00:00 → 2026-11-19T11:10:39+00:00
| − | <language>Scheme</language> |
| − | <code id="506b0f9020599" hidden> |
| − | (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)) |
| − | 19860617)))) ;; an arbitrarily chosen birthday |
| − | (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)))))))) |
| − | |
| − | ;(define random |
| − | ; (let ((seed (time-nanosecond (current-time)))) |
| − | ; (display seed) |
| − | ; (random-maker seed))) |
| − | |
| − | (require-library 'sisc/libs/srfi/srfi-19) |
| − | (import srfi-19) ; stellt current-time usw. bereit |
| − | |
| − | (define random (random-maker (time-nanosecond (current-time)))) |
| − | </code> |
| − | |
| − | === Intervalltest für Zufallszahlen === |
| − | |
| − | <code id="506b0f902061d"> |
| − | (define random-test |
| − | (lambda (n max) |
| − | (if (= max 0) |
| − | 'ok |
| − | (let [(z (random n))] |
| − | (if (or (< z 0) (> z (- n 1))) |
| − | (list z 'error) |
| − | (random-test n (- max 1))))))) |
| − | </code> |
| − | |
| − | <run id="506b0f90206bb"> |
| − | (random-test 10 1000) |
| + | <language>Scheme</language> |
| + | <code id="b4834ea770070" hidden> |
| + | (define modulo |
| + | (lambda (m n) |
| + | (let ([k (floor (/ m n))]) |
| + | (round (* (- (/ m n) k) n))))) |
| + | </code> |
| + | |
| + | === Intervalltest für Zufallszahlen === |
| + | |
| + | <code id="a3df3ea891d13"> |
| + | (define random-test |
| + | (lambda (n max) |
| + | (if (= max 0) |
| + | 'ok |
| + | (let [(z (random n))] |
| + | (if (or (< z 0) (> z (- n 1))) |
| + | (string-append (number->string z) " --> Fehler!") |
| + | (random-test n (- max 1))))))) |
| + | </code> |
| + | |
| + | <run id="30db9959c45df"> |
| + | (random-test 10 1000) |
| + | </run> |
| + | |
| + | <run id="1387a1639e816"> |
| + | (+ 1 (random 10)) |
</run> |