From dd5e517e882de0d68ca88098b94e24438c3ccd1a Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Thu, 29 Aug 2019 19:14:39 -0600 Subject: [PATCH] cs: fix `random-seed` to be compatible with Racket Fix `random-seed` to set the pseudo-random generator's state to the same state as traditional Racket. --- racket/src/cs/rumble/random.ss | 44 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/racket/src/cs/rumble/random.ss b/racket/src/cs/rumble/random.ss index e938b0ef56..bc235bed7c 100644 --- a/racket/src/cs/rumble/random.ss +++ b/racket/src/cs/rumble/random.ss @@ -90,15 +90,17 @@ (srand-half! s (bitwise-and (bitwise-arithmetic-shift-right x 16) #xFFFF))) (define (srand-half! s x) - (let* ([x (random-n! x + (let* ([u32+ (lambda (a b) + (bitwise-and (+ a b) #xFFFFFFFF))] + [x (random-n! x (- Im1 1) (lambda (z) (pseudo-random-generator-x10-set! s (exact->inexact (+ 1 (modulo - (+ (inexact->exact (pseudo-random-generator-x10 s)) - z) + (u32+ (inexact->exact (pseudo-random-generator-x10 s)) + z) (- Im1 1)))))))] [x (random-n! x Im1 @@ -106,20 +108,20 @@ (pseudo-random-generator-x11-set! s (exact->inexact - (+ 1 (modulo - (+ (inexact->exact (pseudo-random-generator-x11 s)) - z) - Im1))))))] + (modulo + (u32+ (inexact->exact (pseudo-random-generator-x11 s)) + z) + Im1)))))] [x (random-n! x Im1 (lambda (z) (pseudo-random-generator-x12-set! s (exact->inexact - (+ 1 (modulo - (+ (inexact->exact (pseudo-random-generator-x12 s)) - z) - Im1))))))] + (modulo + (u32+ (inexact->exact (pseudo-random-generator-x12 s)) + z) + Im1)))))] [x (random-n! x (- Im2 1) (lambda (z) @@ -127,8 +129,8 @@ s (exact->inexact (+ 1 (modulo - (+ (inexact->exact (pseudo-random-generator-x20 s)) - z) + (u32+ (inexact->exact (pseudo-random-generator-x20 s)) + z) (- Im2 1)))))))] [x (random-n! x Im2 @@ -136,20 +138,20 @@ (pseudo-random-generator-x21-set! s (exact->inexact - (+ 1 (modulo - (+ (inexact->exact (pseudo-random-generator-x21 s)) - z) - Im2))))))] + (modulo + (u32+ (inexact->exact (pseudo-random-generator-x21 s)) + z) + Im2)))))] [x (random-n! x Im2 (lambda (z) (pseudo-random-generator-x22-set! s (exact->inexact - (+ 1 (modulo - (+ (inexact->exact (pseudo-random-generator-x22 s)) - z) - Im2))))))]) + (modulo + (u32+ (inexact->exact (pseudo-random-generator-x22 s)) + z) + Im2)))))]) (void))) (define (random-n! x Im k)