From 1fb756d647663737385080b5579a218687e88828 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Sat, 14 Sep 2024 12:11:33 -0400 Subject: [PATCH] use a better random number generator from CHICKEN --- markov.chicken.scm | 8 ++------ markov.impl.scm | 20 +++++++++++--------- toplevel.scm | 11 +++++++---- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/markov.chicken.scm b/markov.chicken.scm index ba3453f..d079063 100644 --- a/markov.chicken.scm +++ b/markov.chicken.scm @@ -3,16 +3,12 @@ guess-sentence generate-next-word join-verses - %random-seed seed-from-entropy) (import scheme (chicken base) srfi-69 (chicken format) - (chicken random) - srfi-4) + (chicken random)) (define (seed-from-entropy) - (let ((bytes (blob->u8vector (random-bytes)))) - (set! %random-seed (+ (u8vector-ref bytes 0) - (* 255 (u8vector-ref bytes 1)))))) + (set-pseudo-random-seed! (random-bytes))) (define (join-verses* lst str book chapter verse) (if (null? lst) str diff --git a/markov.impl.scm b/markov.impl.scm index c5e0423..bb67955 100644 --- a/markov.impl.scm +++ b/markov.impl.scm @@ -169,15 +169,17 @@ ;;; Lehmer random number generator -(define %random-seed 10000) - -(define random - (let ((m 65537) - (a 75)) - (lambda () - (set! %random-seed (remainder (* a %random-seed) - m)) - (/ %random-seed (- m 1))))) +(cond-expand + (chicken (import (chicken random)) + (define random generate-random-real)) + (else (define %random-seed 10000) + (define random + (let ((m 65537) + (a 75)) + (lambda () + (set! %random-seed (remainder (* a %random-seed) + m)) + (/ %random-seed (- m 1))))))) (define probability-list->guess (lambda (lst normalized-random-number) diff --git a/toplevel.scm b/toplevel.scm index 763eaf5..9fa11bd 100644 --- a/toplevel.scm +++ b/toplevel.scm @@ -37,10 +37,13 @@ (thread-sleep! time) (cond ((equal? previous-word "startofbook") - (printf "THE BOOK OF ~A~%~%~%" (generate-next-word table "of")) - (printf "~A:1:1" (+ book 1)) - (generate-continuously* (generate-next-word table "startofverse") - time (+ 1 book) 1 1)) + (let ((next-word (generate-next-word table "of"))) + (if (equal? next-word "the") + (set! next-word (generate-next-word table "the"))) + (printf "THE BOOK OF ~A~%~%~%" next-word) + (printf "~A:1:1" (+ book 1)) + (generate-continuously* (generate-next-word table "startofverse") + time (+ 1 book) 1 1))) ((equal? previous-word "startofchapter") (printf "~%~%~A:~A:1" book (+ 1 chapter)) (generate-continuously* (generate-next-word table "startofverse")