Elementary cellular automaton/Random number generator: Difference between revisions
Elementary cellular automaton/Random number generator (view source)
Revision as of 08:26, 13 January 2015
, 9 years ago→{{header|Racket}}: conforming output
(→{{header|Racket}}: conforming output) |
|||
Line 232:
;; This is the RNG automaton
(define
#:rule (rule 30) ; rule 30 is random, maybe you're interested in using others
;; width of the CA... this is implemented as a number of words plus, maybe, another word
;; containing the spare bits
#:bits (bits 256))
([wrap-rule] (and (positive? more-bits) (wrap-rule-truncate-left-word more-bits)))
([next-gen] (CA-next-generation 30 #:wrap-rule wrap-rule))
([v] (make-fxvector (+ full-words (if more-bits 1 0)))))
(fxvector-set! v 0 1) ; this bit will always have significance
(define (next-word)
(define-values (v+ o) (
(
(fxvector-ref v 0
(lambda (bits)
(for/fold ((acc 0)) ((_ (in-range bits)))
▲ ((_ (in-range 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
;; To match the other examples on this page, the automaton is 30+30+4 bits long
;; (
(define C30-rand-64 (CA30-random-generator #:bits 64))
;; this should be the list from "C"
(for/list ((i 10)) (C30-rand-64 8))
; we also do big numbers...
(number->string (
(number->string (
(number->string (
(number->string (
{{out}}
<pre>(220 197 147 174 117 97 149 171 100 151)
"ecd9fbcdcc34604d833950deb58447124b98706e74ccc74d9337cb4e53f38c5e"
"9c8b6471a4bc2cb3508f10b6635e4eb959ad8bbe484480695e8ddb5795f956a"
"6d85153a987dad6f013bc6159a41bf95b9d9b14af87733e17c702a3dc9052172"
"fc6fd302f5ea8f2fba6f476cfe9d090dc877dbd558e5afba49044d05b14d258"</pre>
=={{header|Ruby}}==
|