Functions to generate random numbers and data structures.
emacs 27.1
higher
- Predictable random number generation
- Predictable lazy random ranges generation
- Predictable random lists generation
Example ~/.emacs
config:
;;; -*- lexical-binding: t; -*-
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
;; The extension file content with all comments removed can be placed here.
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages '(markdown-mode)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
All from
, to
and length
parameters are optional and default to the following values respectively: 0
, 100
, and 10
.
By convention all parameters are ordered as follows:
length
,from
, andto
(for functions accepting random number ranges)sequence
, andlength
(for functions accepting another sequence)
Functions ending with -from
generate sequences or lists from items of existing sequences.
Below square brackets are used to represent optional parameters and not arrays.
- Function:
random-generators-number [from to]
Description: Generate a number inside specific boundaries. - Function:
random-generators-sequence [length from to]
Description: Generate a sequence with a specific length. - Function:
random-generators-list [length from to]
Description: Generate a list with a specific length. - Function:
random-generators-sequence-from sequence [length]
Description: Generate a sequence with a specific length from another one. - Function:
random-generators-sequence-from-or-empty sequence [length]
Description: Try generate a sequence with a specific length from another one. - Function:
random-generators-list-from sequence [length]
Description: Generate a list with a specific length from another one. - Function:
random-generators-list-from-or-empty sequence [length]
Description: Try generate a list with a specific length from another one.
Generate a random number within [10..20]
range:
(random-generators-number 10 20)
Generate a sequence of 5 random numbers within [10..20]
range:
(iter-do (item (random-generators-sequence 5 10 20)) (message (format "item is %d" item)))
Generate a list of 5 random numbers within [10..20]
range:
(random-generators-list 5 10 20)
Generate a sequence of 5 randomly choosen numbers from a list:
(iter-do (item (random-generators-sequence-from '(2 4 6 8) 5)) (message (format "item is %d" item)))
Note random-generators-sequence-from-or-empty
can be used if instead of non-empty sequence like '(2 4 6 8)
you pass possibly empty one as
in this case no error will be thown for '()
. The same rule applies to random-generators-list-from
function too.
- Why provided functions are more predictable than standard ones?
Because they explicitly mandate user to pass just integer arguments for range borders and
unlike
random
don't generate unconstrained random numbers withLIMIT
< 0 which can be unexpected behavior. - Why to use generators to generate random sequences? As long as sequences can be extremely long not to consume too much memory it's better to use generators.