2019-10-09 13:13:56 +02:00
|
|
|
;;; random.el --- Functions for working with randomness -*- lexical-binding: t -*-
|
2020-08-31 15:51:27 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-08-31 15:51:27 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24"))
|
|
|
|
;; Homepage: https://user.git.corp.google.com/wpcarro/briefcase
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Functions for working with randomness. Some of this code is not as
|
|
|
|
;; functional as I'd like from.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2020-08-31 15:51:27 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'prelude)
|
|
|
|
(require 'number)
|
|
|
|
(require 'math)
|
|
|
|
(require 'series)
|
|
|
|
(require 'list)
|
|
|
|
(require 'set)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:51:27 +02:00
|
|
|
(defun random-int (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a random integer from 0 to `X'."
|
|
|
|
(random x))
|
|
|
|
|
|
|
|
;; TODO: Make this work with sequences instead of lists.
|
2020-08-31 15:51:27 +02:00
|
|
|
(defun random-choice (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a random element of `XS'."
|
|
|
|
(let ((ct (list/length xs)))
|
|
|
|
(list/get
|
2020-08-31 15:51:27 +02:00
|
|
|
(random-int ct)
|
2019-10-09 13:13:56 +02:00
|
|
|
xs)))
|
|
|
|
|
2020-08-31 15:51:27 +02:00
|
|
|
(defun random-boolean? ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Randonly return t or nil."
|
2020-08-31 15:51:27 +02:00
|
|
|
(random-choice (list t nil)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: This may not work if any of these generate numbers like 0, 1, etc.
|
2020-08-31 15:51:27 +02:00
|
|
|
(defun random-uuid ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a generated UUID string."
|
|
|
|
(let ((eight (number/dec (math/triangle-of-power :base 16 :power 8)))
|
|
|
|
(four (number/dec (math/triangle-of-power :base 16 :power 4)))
|
|
|
|
(twelve (number/dec (math/triangle-of-power :base 16 :power 12))))
|
|
|
|
(format "%x-%x-%x-%x-%x"
|
2020-08-31 15:51:27 +02:00
|
|
|
(random-int eight)
|
|
|
|
(random-int four)
|
|
|
|
(random-int four)
|
|
|
|
(random-int four)
|
|
|
|
(random-int twelve))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:51:27 +02:00
|
|
|
(defun random-token (length)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a randomly generated hexadecimal string of LENGTH."
|
|
|
|
(->> (series/range 0 (number/dec length))
|
2020-08-31 15:51:27 +02:00
|
|
|
(list/map (lambda (_) (format "%x" (random-int 15))))
|
2019-10-09 13:13:56 +02:00
|
|
|
(list/join "")))
|
|
|
|
|
2020-08-31 15:51:27 +02:00
|
|
|
;; TODO: Support random-sample
|
|
|
|
;; (defun random-sample (n xs)
|
|
|
|
;; "Return a randomly sample of list XS of size N."
|
2020-08-31 18:05:31 +02:00
|
|
|
;; (prelude-assert (and (>= n 0) (< n (list/length xs))))
|
2020-08-31 15:51:27 +02:00
|
|
|
;; (cl-labels ((do-sample
|
|
|
|
;; (n xs y ys)
|
|
|
|
;; (if (= n (set/count ys))
|
|
|
|
;; (->> ys
|
|
|
|
;; set/to-list
|
|
|
|
;; (list/map (lambda (i)
|
|
|
|
;; (list/get i xs))))
|
|
|
|
;; (if (set/contains? y ys)
|
|
|
|
;; (do-sample n xs (random-int (list/length xs)) ys)
|
|
|
|
;; (do-sample n xs y (set/add y ys))))))
|
|
|
|
;; (do-sample n xs (random-int (list/length xs)) (set/new))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'random)
|
|
|
|
;;; random.el ends here
|