䷬ 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."
(interactive)
(message "casting hexagram using: %s"
(if method method i-ching-divination-method))
(let ((method-function
(when (not method)
(let ((method i-ching-divination-method))
(message "casting hexagram using: %s" method)
(pcase method
(pcase (or method i-ching-divination-method)
;; the casting method should return a hexagram (or changing hexagram)
('3-coins #'i-ching--three-coins)
('yarrow-stalks #'i-ching--yarrow-stalks)
@ -582,7 +581,7 @@ see: `i-ching-divination-method' & `i-ching-randomness-source' for details."
('bagua (message "unimplemented"))
('cheezburger (message "LOL"))
(_
#'i-ching--random-number))))))
#'i-ching--random-number))))
(when method-function
(let* ((line-1 (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)
"Return a random integer from 1 to N inclusive (possibly with a specific SOURCE of randomness)."
(when (not source) (setq source i-ching-randomness-source))
(message "randomness source: %s" source)
(pcase source
('quantum (pcase n
(64 (i-ching-q64))
(_ (/ (i-ching-q64) (/ 64 n)))))
('atmospheric (pcase n
(64 (i-ching-r64))
(_ (/ (i-ching-r64) (/ 64 n)))))
('pseudo (+ 1 (random n)))
(_
(+ 1 (random n)))))
"Return a random integer from 1 to N inclusive.
Optionally with a specific SOURCE of randomness."
(message "Using randomness source: %s"
(if source source i-ching-randomness-source))
(+ 1
(mod
(pcase (or source i-ching-randomness-source)
('quantum (i-ching-q64))
('atmospheric (i-ching-r64))
('pseudo (random n))
(_ (random n)))
n)))
(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/"
(let ((numeric 0))
(request
@ -701,13 +698,12 @@ Provided by ANU QRNG via https://qrng.anu.edu.au/"
4)))))
numeric))
(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/"
(let ((numeric 0))
(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
;; :parser 'json-read
:success (cl-function

View file

@ -1,23 +1,32 @@
;;; tests.el --- tests for i-ching.el -*- coding: utf-8; lexical-binding: t -*-
;;; Commentary:
;;
;; some simple tests...
;;; Code:
(require 'ert)
(require 'with-simulated-input)
(require 'i-ching)
;; range 0-63 NOTE: doesn't fail when rate-limit is hit
(ert-deftest i-ching-qrnd ()
(should (numberp (i-ching-q64)))
(should (< 0 (i-ching-q64)))
(should (>= 64 (i-ching-q64))))
(should (<= 0 (i-ching-q64)))
(should (> 64 (i-ching-q64))))
;; range 0-63
(ert-deftest i-ching-rrnd ()
(should (numberp (i-ching-r64)))
(should (< 0 (i-ching-r64)))
(should (>= 64 (i-ching-r64))))
(should (<= 0 (i-ching-r64)))
(should (> 64 (i-ching-r64))))
(ert-deftest i-ching-random ()
(should (numberp (i-ching-random 'quantum)))
(should (numberp (i-ching-random 'atmospheric)))
(should (numberp (i-ching-random 'pseudo))))
(should (numberp (i-ching-random 64 'quantum)))
(should (numberp (i-ching-random 64 'atmospheric)))
(should (numberp (i-ching-random 64 'pseudo)))
(should (numberp (i-ching-random 64))))
(ert-deftest i-ching-n2h ()
(should (string= "" (i-ching-number-to-hexagram 2)))
@ -34,6 +43,7 @@
(ert-deftest i-ching-names ()
(should-not (i-ching-number-to-name 0))
(should-not (i-ching-number-to-name 65))
(should (string= "HEXAGRAM FOR BEFORE COMPLETION"
(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-judgment 27)))
(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-judgment 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))))
;; 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 '3-coins)
;; (i-ching-query 'yarrow-stalks)
;; interactive testing
(ert-deftest i-ching-query-6bit ()
(should (with-simulated-input
"testing SPC circumstances RET"