-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandom.ark
More file actions
32 lines (29 loc) · 777 Bytes
/
Random.ark
File metadata and controls
32 lines (29 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# @brief Select a random element from a list
# @details If the list is empty, returns nil
# @param _L list of elements
# =begin
# (let data [1 2 3 4 5])
# (print (random:choice data))
# =end
# @author https://github.com/SuperFola
(let choice (fun (_L)
(if (empty? _L)
nil
(if (= 1 (len _L))
(head _L)
(@ _L (random 0 (- (len _L) 1)))))))
# @brief Shuffle a given list
# @details The original list is not modified
# @param _L list to shuffle
# =begin
# (let data [1 2 3 4 5])
# (let randomized (random:shuffle data))
# =end
# @author https://github.com/SuperFola
(let shuffle (fun ((mut _L)) {
(mut _output [])
(while (not (empty? _L)) {
(let _idx (random 0 (- (len _L) 1)))
(append! _output (@ _L _idx))
(pop! _L _idx) })
_output }))