fork(2) download
  1. ; reversible random number generator
  2.  
  3. (define (euclid x y)
  4. (let loop ((a 1) (b 0) (g x) (u 0) (v 1) (w y))
  5. (if (zero? w) (values a b g)
  6. (let ((q (quotient g w)))
  7. (loop u v w (- a (* q u)) (- b (* q v)) (- g (* q w)))))))
  8.  
  9. (define (inverse x m)
  10. (if (not (= (gcd x m) 1))
  11. (error 'inverse "divisor must be coprime to modulus")
  12. (call-with-values
  13. (lambda () (euclid x m))
  14. (lambda (a b g) (modulo a m)))))
  15.  
  16. (define next #f)
  17. (define prev #f)
  18.  
  19. (let ((a 69069) (c 1234567) (m (expt 2 32)))
  20. (set! next (lambda (x) (modulo (+ (* a x) c) m)))
  21. (set! prev (lambda (x) (modulo (* (inverse a m) (- x c)) m))))
  22.  
  23. (display (next 2718281828)) (newline)
  24. (display (next 3103402651)) (newline)
  25. (display (next 4281062310)) (newline)
  26. (display (prev 1670430837)) (newline)
  27. (display (prev 4281062310)) (newline)
  28. (display (prev 3103402651)) (newline)
Success #stdin #stdout 0.03s 8616KB
stdin
Standard input is empty
stdout
3103402651
4281062310
1670430837
4281062310
3103402651
2718281828