use a better random number generator from CHICKEN

This commit is contained in:
Peter McGoron 2024-09-14 12:11:33 -04:00
parent dd1f97484a
commit 1fb756d647
3 changed files with 20 additions and 19 deletions

View File

@ -3,16 +3,12 @@
guess-sentence guess-sentence
generate-next-word generate-next-word
join-verses join-verses
%random-seed
seed-from-entropy) seed-from-entropy)
(import scheme (chicken base) srfi-69 (import scheme (chicken base) srfi-69
(chicken format) (chicken format)
(chicken random) (chicken random))
srfi-4)
(define (seed-from-entropy) (define (seed-from-entropy)
(let ((bytes (blob->u8vector (random-bytes)))) (set-pseudo-random-seed! (random-bytes)))
(set! %random-seed (+ (u8vector-ref bytes 0)
(* 255 (u8vector-ref bytes 1))))))
(define (join-verses* lst str book chapter verse) (define (join-verses* lst str book chapter verse)
(if (null? lst) (if (null? lst)
str str

View File

@ -169,15 +169,17 @@
;;; Lehmer random number generator ;;; Lehmer random number generator
(define %random-seed 10000) (cond-expand
(chicken (import (chicken random))
(define random generate-random-real))
(else (define %random-seed 10000)
(define random (define random
(let ((m 65537) (let ((m 65537)
(a 75)) (a 75))
(lambda () (lambda ()
(set! %random-seed (remainder (* a %random-seed) (set! %random-seed (remainder (* a %random-seed)
m)) m))
(/ %random-seed (- m 1))))) (/ %random-seed (- m 1)))))))
(define probability-list->guess (define probability-list->guess
(lambda (lst normalized-random-number) (lambda (lst normalized-random-number)

View File

@ -37,10 +37,13 @@
(thread-sleep! time) (thread-sleep! time)
(cond (cond
((equal? previous-word "startofbook") ((equal? previous-word "startofbook")
(printf "THE BOOK OF ~A~%~%~%" (generate-next-word table "of")) (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)) (printf "~A:1:1" (+ book 1))
(generate-continuously* (generate-next-word table "startofverse") (generate-continuously* (generate-next-word table "startofverse")
time (+ 1 book) 1 1)) time (+ 1 book) 1 1)))
((equal? previous-word "startofchapter") ((equal? previous-word "startofchapter")
(printf "~%~%~A:~A:1" book (+ 1 chapter)) (printf "~%~%~A:~A:1" book (+ 1 chapter))
(generate-continuously* (generate-next-word table "startofverse") (generate-continuously* (generate-next-word table "startofverse")