Anonymous user
Elementary cellular automaton/Random number generator: Difference between revisions
Elementary cellular automaton/Random number generator (view source)
Revision as of 17:13, 9 March 2015
, 9 years ago→{{header|Racket}}: Improve code layout
(→{{header|Racket}}: conforming output) |
m (→{{header|Racket}}: Improve code layout) |
||
Line 227:
<lang racket>#lang racket
;; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
(require racket/fixnum)
Line 233:
;; This is the RNG automaton
(define (CA30-random-generator
#:rule
;; width of the CA... this is implemented as a number of words
;; maybe, another word containing the spare bits
#:bits
(
(quotient/remainder bits usable-bits/fixnum))
([next-gen] (CA-next-generation 30 #:wrap-rule wrap-rule))▼
(define v (make-fxvector (+ full-words (if more-bits 1 0))))
▲ (define (next-word)
(define-values [v+ o] (next-gen v 0))
(lambda (bits)
(
▲ ;; the CA is fixnum, but this function returns integers of arbitrary width
▲ (bitwise-ior (arithmetic-shift acc 1) (bitwise-and (next-word) 1))))))
(module+ main
Line 260 ⟶ 259:
(define C30-rand-64 (CA30-random-generator #:bits 64))
;; this should be the list from "C"
(for/list (
; we also do big numbers...
(number->string (C30-rand-64 256) 16)
|