Skip to content

Instantly share code, notes, and snippets.

@ryukinix
Created August 15, 2020 06:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryukinix/c87b39fffd64b2798f4a8ed961c0e2d0 to your computer and use it in GitHub Desktop.
Save ryukinix/c87b39fffd64b2798f4a8ed961c0e2d0 to your computer and use it in GitHub Desktop.
;; pseudo-random numbers don't follows Benford's Law.
(ql:quickload :cl-spark)
(defun random-numbers (max count)
(loop repeat count
for n := (random max)
when (plusp n)
collect n))
(defun most-significant-digit (x)
(nth-value 0 (floor (/ x (expt 10 (floor (log x 10)))))))
(defun histogram (numbers)
(loop with hist := (make-array 9 :initial-element 0)
for x in numbers
do (incf (aref hist (1- x)))
finally (return hist)))
(defun main (&optional (max 1000000) (count 1000000))
(let* ((numbers (random-numbers max count))
(digits (mapcar #'most-significant-digit numbers))
(hist (histogram digits)))
(spark:spark (coerce hist 'list))))
#|
CL-USER> (main)
"▅▇▇▆▄▁▄█▄"
CL-USER> (main)
"▆▅▂▁▆▁▂▁█"
CL-USER> (main)
"▄▁█▁▃▅▄▄▂"
CL-USER> (main)
"▃▅▂▁▁█▂▃▂"
CL-USER> (main)
"▁▆▅█▄▂▆▅▅"
CL-USER> (main)
"▂▃▅█▂▁▃▂▂"
CL-USER> (main)
"▅▅▃▁▂█▅▃▁"
CL-USER> (main)
"█▆▅▃▇▃▃▃▁"
CL-USER> (main)
"▂▇▇█▇▃▇▃▁"
CL-USER> (main)
"▇▇▂▁▄▇▄█▆"
CL-USER> (main)
"▆▁▄▂▃█▆▆▇"
CL-USER> (main)
"▁▁▆▂▃▂▃█▇"
CL-USER> (main)
"▂▄▄█▁▆▂▄▆"
CL-USER> (main)
"▅▄▇▅▄█▁▃▂"
CL-USER> (main)
"▁▅▄▃▃▄▂▄█"
CL-USER> (main)
"▃▅▂▃█▅▁▃▆"
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment