䷬ Gathering Together

This commit is contained in:
nik gaffney 2023-05-26 13:12:57 +02:00
parent 54f19e2dcb
commit ab9eaa445d
Signed by: nik
GPG key ID: 989F5E6EDB478160
2 changed files with 436 additions and 412 deletions

View file

@ -569,11 +569,10 @@ Depending on the context and circumstance some methods may be more suitable.
see: `i-ching-divination-method' & `i-ching-randomness-source' for details." see: `i-ching-divination-method' & `i-ching-randomness-source' for details."
(interactive) (interactive)
(message "casting hexagram using: %s"
(if method method i-ching-divination-method))
(let ((method-function (let ((method-function
(when (not method) (pcase (or method i-ching-divination-method)
(let ((method i-ching-divination-method))
(message "casting hexagram using: %s" method)
(pcase method
;; the casting method should return a hexagram (or changing hexagram) ;; the casting method should return a hexagram (or changing hexagram)
('3-coins #'i-ching--three-coins) ('3-coins #'i-ching--three-coins)
('yarrow-stalks #'i-ching--yarrow-stalks) ('yarrow-stalks #'i-ching--yarrow-stalks)
@ -582,7 +581,7 @@ see: `i-ching-divination-method' & `i-ching-randomness-source' for details."
('bagua (message "unimplemented")) ('bagua (message "unimplemented"))
('cheezburger (message "LOL")) ('cheezburger (message "LOL"))
(_ (_
#'i-ching--random-number)))))) #'i-ching--random-number))))
(when method-function (when method-function
(let* ((line-1 (funcall method-function)) (let* ((line-1 (funcall method-function))
(line-2 (funcall method-function)) (line-2 (funcall method-function))
@ -669,23 +668,21 @@ see: `i-ching-divination-method' & `i-ching-randomness-source' for details."
;;; ; ; ;; ;;; ; ; ;;
(defun i-ching-random (n &optional source) (defun i-ching-random (n &optional source)
"Return a random integer from 1 to N inclusive (possibly with a specific SOURCE of randomness)." "Return a random integer from 1 to N inclusive.
(when (not source) (setq source i-ching-randomness-source)) Optionally with a specific SOURCE of randomness."
(message "randomness source: %s" source) (message "Using randomness source: %s"
(pcase source (if source source i-ching-randomness-source))
('quantum (pcase n (+ 1
(64 (i-ching-q64)) (mod
(_ (/ (i-ching-q64) (/ 64 n))))) (pcase (or source i-ching-randomness-source)
('atmospheric (pcase n ('quantum (i-ching-q64))
(64 (i-ching-r64)) ('atmospheric (i-ching-r64))
(_ (/ (i-ching-r64) (/ 64 n))))) ('pseudo (random n))
('pseudo (+ 1 (random n))) (_ (random n)))
(_ n)))
(+ 1 (random n)))))
(defun i-ching-q64 () (defun i-ching-q64 ()
"Genuine Quantum Randomness™ from quantum fluctuations of the vacuum [1..64]. "Genuine Quantum Randomness™ from quantum fluctuations of the vacuum [0..63].
Provided by ANU QRNG via https://qrng.anu.edu.au/" Provided by ANU QRNG via https://qrng.anu.edu.au/"
(let ((numeric 0)) (let ((numeric 0))
(request (request
@ -701,13 +698,12 @@ Provided by ANU QRNG via https://qrng.anu.edu.au/"
4))))) 4)))))
numeric)) numeric))
(defun i-ching-r64 () (defun i-ching-r64 ()
"True random numbers from atmospheric noise [1..64]. "True random numbers from atmospheric noise [0..63].
Provided by Randomness and Integrity Services Ltd. via https://www.random.org/" Provided by Randomness and Integrity Services Ltd. via https://www.random.org/"
(let ((numeric 0)) (let ((numeric 0))
(request (request
"https://www.random.org/integers/?num=1&min=1&max=64&col=1&base=10&format=plain&rnd=new" "https://www.random.org/integers/?num=1&min=0&max=63&col=1&base=10&format=plain&rnd=new"
:sync t :sync t
;; :parser 'json-read ;; :parser 'json-read
:success (cl-function :success (cl-function

View file

@ -1,23 +1,32 @@
;;; tests.el --- tests for i-ching.el -*- coding: utf-8; lexical-binding: t -*- ;;; tests.el --- tests for i-ching.el -*- coding: utf-8; lexical-binding: t -*-
;;; Commentary:
;;
;; some simple tests... ;; some simple tests...
;;; Code:
(require 'ert)
(require 'with-simulated-input)
(require 'i-ching) (require 'i-ching)
;; range 0-63 NOTE: doesn't fail when rate-limit is hit
(ert-deftest i-ching-qrnd () (ert-deftest i-ching-qrnd ()
(should (numberp (i-ching-q64))) (should (numberp (i-ching-q64)))
(should (< 0 (i-ching-q64))) (should (<= 0 (i-ching-q64)))
(should (>= 64 (i-ching-q64)))) (should (> 64 (i-ching-q64))))
;; range 0-63
(ert-deftest i-ching-rrnd () (ert-deftest i-ching-rrnd ()
(should (numberp (i-ching-r64))) (should (numberp (i-ching-r64)))
(should (< 0 (i-ching-r64))) (should (<= 0 (i-ching-r64)))
(should (>= 64 (i-ching-r64)))) (should (> 64 (i-ching-r64))))
(ert-deftest i-ching-random () (ert-deftest i-ching-random ()
(should (numberp (i-ching-random 'quantum))) (should (numberp (i-ching-random 64 'quantum)))
(should (numberp (i-ching-random 'atmospheric))) (should (numberp (i-ching-random 64 'atmospheric)))
(should (numberp (i-ching-random 'pseudo)))) (should (numberp (i-ching-random 64 'pseudo)))
(should (numberp (i-ching-random 64))))
(ert-deftest i-ching-n2h () (ert-deftest i-ching-n2h ()
(should (string= "" (i-ching-number-to-hexagram 2))) (should (string= "" (i-ching-number-to-hexagram 2)))
@ -34,6 +43,7 @@
(ert-deftest i-ching-names () (ert-deftest i-ching-names ()
(should-not (i-ching-number-to-name 0))
(should-not (i-ching-number-to-name 65)) (should-not (i-ching-number-to-name 65))
(should (string= "HEXAGRAM FOR BEFORE COMPLETION" (should (string= "HEXAGRAM FOR BEFORE COMPLETION"
(i-ching-number-to-unicode-name 64))) (i-ching-number-to-unicode-name 64)))
@ -44,6 +54,9 @@
(should (stringp (i-ching-number-to-description 43))) (should (stringp (i-ching-number-to-description 43)))
(should (stringp (i-ching-number-to-judgment 27))) (should (stringp (i-ching-number-to-judgment 27)))
(should (stringp (i-ching-number-to-image 13))) (should (stringp (i-ching-number-to-image 13)))
(should-not (stringp (i-ching-number-to-description 0)))
(should-not (stringp (i-ching-number-to-judgment 0)))
(should-not (stringp (i-ching-number-to-image 0)))
(should-not (stringp (i-ching-number-to-description 65))) (should-not (stringp (i-ching-number-to-description 65)))
(should-not (stringp (i-ching-number-to-judgment 65))) (should-not (stringp (i-ching-number-to-judgment 65)))
(should-not (stringp (i-ching-number-to-image 65)))) (should-not (stringp (i-ching-number-to-image 65))))
@ -74,12 +87,27 @@
(should (stringp (i-ching-cast '3-coins))) (should (stringp (i-ching-cast '3-coins)))
(should (stringp (i-ching-cast)))) (should (stringp (i-ching-cast))))
;; randomness
(defun χ² (expected observed)
"Chi-squared test for EXPECTED OBSERVED sequences."
(cl-reduce #'+
(cl-mapcar
(lambda (e o) (/ (expt (- o e) 2) e))
expected observed)))
;; (mapcar (lambda (n)
;; (i-ching-random n 'atmospheric))
;; (make-list 1024 64))
;; interactive testing
;; (i-ching-query '6-bit) ;; (i-ching-query '6-bit)
;; (i-ching-query '3-coins) ;; (i-ching-query '3-coins)
;; (i-ching-query 'yarrow-stalks) ;; (i-ching-query 'yarrow-stalks)
;; interactive testing
(ert-deftest i-ching-query-6bit () (ert-deftest i-ching-query-6bit ()
(should (with-simulated-input (should (with-simulated-input
"testing SPC circumstances RET" "testing SPC circumstances RET"