networks: Split the randomness functions into their own section.

This commit is contained in:
Sergiu Ivanov 2020-03-20 22:22:33 +01:00
parent 6c75a073c0
commit bba2dfad12
2 changed files with 14 additions and 8 deletions

View File

@ -224,12 +224,13 @@
(let ([f1 (stream-first (enumerate-boolean-functions 1))]
[f1/list (stream-first (enumerate-boolean-functions/list 1))])
(check-false (f1 #f)) (check-false (f1 #t))
(check-false (f1/list '(#f))) (check-false (f1/list '(#t))))
(check-false (f1/list '(#f))) (check-false (f1/list '(#t)))))
(random-seed 0)
(check-equal? (random-boolean-table 2) '((#f #f #t) (#f #t #t) (#t #f #f) (#t #t #f)))
(let ([f (random-boolean-function 2)])
(check-true (f #f #f)) (check-false (f #f #t)) (check-true (f #t #f)) (check-false (f #t #t)))
(let ([f (random-boolean-function/list 2)])
(check-false (f '(#f #f))) (check-true (f '(#f #t)))
(check-true (f '(#t #f))) (check-false (f '(#t #t)))))
(test-case "Random Boolean functions and networks"
(random-seed 0)
(check-equal? (random-boolean-table 2) '((#f #f #t) (#f #t #t) (#t #f #f) (#t #t #f)))
(let ([f (random-boolean-function 2)])
(check-true (f #f #f)) (check-false (f #f #t)) (check-true (f #t #f)) (check-false (f #t #t)))
(let ([f (random-boolean-function/list 2)])
(check-false (f '(#f #f))) (check-true (f '(#f #t)))
(check-true (f '(#t #f))) (check-false (f '(#t #t)))))

View File

@ -477,6 +477,11 @@
(define (enumerate-boolean-functions/list n)
(stream-map table->function/list (enumerate-boolean-tables n)))
;;; =====================================
;;; Random Boolean functions and networks
;;; =====================================
;;; Generates a random truth table for a Boolean function of arity n.
(define (random-boolean-table n)
(define/match (num->bool x) [(0) #f] [(1) #t])