Add Licensing and Contributing
[clnl] / src / main / clnl / nvm / math.lisp
1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3.  See distributed LICENSE.txt.
2 (in-package #:clnl-nvm)
3
4 (defun random-float (n)
5  "RANDOM-FLOAT N => RANDOM-NUMBER
6
7 ARGUMENTS AND VALUES:
8
9   N: a double, the upper bound of the random float
10   RANDOM-NUMBER: a double, the random result
11
12 DESCRIPTION:
13
14   Returns a random number strictly closer to zero than N.
15
16   If number is positive, returns a random floating point number greater than
17   or equal to 0 but strictly less than number.
18
19   If number is negative, returns a random floating point number less than or equal
20   to 0, but strictly greater than number.
21
22   If number is zero, the result is always 0.
23
24   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
25  (clnl-random:next-double n))
26
27 (defun random (n)
28  "RANDOM N => RANDOM-NUMBER
29
30 ARGUMENTS AND VALUES:
31
32   N: an integer, the upper bound of the random
33   RANDOM-NUMBER: an integer, the random result
34
35 DESCRIPTION:
36
37   Returns a random number strictly closer to zero than N.
38
39   If number is positive, returns a random integer greater than or equal to 0,
40   but strictly less than number.
41
42   If number is negative, returns a random integer less than or equal to 0,
43   but strictly greater than number.
44
45   If number is zero, the result is always 0.
46
47   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random"
48  (coerce (clnl-random:next-long (truncate n)) 'double-float))