File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ # @brief Select a random element from a list
2
+ # @details If the list is empty, returns nil
3
+ # @param _L list of elements
4
+ # =begin
5
+ # (import std.Random)
6
+ # (print (random:choice [1 2 3]))
7
+ # =end
8
+ # @author https://github.com/SuperFola
9
+ (let choice (fun (_L)
10
+ (if (empty? _L)
11
+ nil
12
+ (if (= 1 (len _L))
13
+ (head _L)
14
+ (@ _L (random 0 (- (len _L) 1)))))))
15
+
Original file line number Diff line number Diff line change 8
8
(import list-tests)
9
9
(import macros-tests)
10
10
(import math-tests)
11
+ (import random-tests)
11
12
(import range-tests)
12
13
(import string-tests)
13
14
(import switch-tests)
26
27
list-tests:list-output
27
28
macros-tests:macros-output
28
29
math-tests:math-output
30
+ random-tests:random-output
29
31
range-tests:range-output
30
32
string-tests:string-output
31
33
switch-tests:switch-output
Original file line number Diff line number Diff line change
1
+ (import std.Random)
2
+ (import std.Testing)
3
+
4
+ (test:suite random {
5
+ (test:case "choice" {
6
+ (test:eq nil (random:choice []))
7
+ (test:eq nil (random:choice ""))
8
+ (test:eq 1 (random:choice [1]))
9
+ (test:eq "1" (random:choice "1"))
10
+
11
+ # have to repeat the test to be sure that we hit every index
12
+ (test:eq 1 (random:choice [1 1 1]))
13
+ (test:eq 1 (random:choice [1 1 1]))
14
+ (test:eq 1 (random:choice [1 1 1]))
15
+ (test:eq 1 (random:choice [1 1 1]))
16
+ (test:eq 1 (random:choice [1 1 1]))
17
+ (test:eq "1" (random:choice "111"))
18
+ (test:eq "1" (random:choice "111"))
19
+ (test:eq "1" (random:choice "111"))
20
+ (test:eq "1" (random:choice "111"))
21
+ (test:eq "1" (random:choice "111")) })})
22
+
You can’t perform that action at this time.
0 commit comments