Skip to content

Commit f9f3417

Browse files
author
Isaac Ramirez
committedSep 24, 2020
- update js docs
1 parent b9fd97a commit f9f3417

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎src/libs/std-random/std-random.js

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class StdRandom {
2828
/**
2929
* Uniform
3030
*
31+
* @todo Somehow I need to make this function to return
32+
* real number values when [0.0, 1.0] are provided.
33+
*
3134
* @constructor
3235
* @desc Returns a random real number uniformly in [0, 1).
3336
* @returns {number} A random real number uniformly in [0, 1).
@@ -66,6 +69,13 @@ class StdRandom {
6669
return getRandomInt(0, lo)
6770
}
6871

72+
/* NOTE: the book uses a signature like `uniform(0.0, 1.0)`
73+
* to generate real or double numbers from [0, 1),
74+
* in case you want create the same behaviour, then do
75+
* `uniform()` and this will make the function to get to this
76+
* returning value. In case you want to generate values from [0.0, 5.0)
77+
* then I would recommend to do `uniform(0, 4.999)`.
78+
*/
6979
return Math.random()
7080
}
7181

‎src/utils/array.utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ function isVector (v) {
1616
return false
1717
}
1818

19+
/**
20+
* Returns an array of length `n` filled with the values
21+
* provided as `data` or computed if `data` is a function.
22+
* @param {number} n
23+
* @param {*|function} data `data` can be any literal value,
24+
* _all the array will be filled with the same value_,
25+
* or a function with signature `(number i)` that will compute
26+
* a value based on the index `i` which is `i >= 0 && i < n`.
27+
* @example <caption>Array of length 10 initialized to 0</caption>
28+
* newArrayOf(10, 0) // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
29+
* @example <caption>Array of length `n` with random values from 0 to `n`</caption>
30+
* const n = 10
31+
* newArrayOf(n, () => StdRandom.uniform(n)) // [0, 3, 9, 2, 2, 8, 4, 2, 4, 7]
32+
* @example <caption>Array of random doubles from [0, 1)</caption>
33+
* newArrayOf(3, () => StdRandom.uniform(0, 0.999)) // [0.712488850522207, 0.9280585621957204, 0.8184198432124032]
34+
* newArrayOf(3, () => StdRandom.uniform()) // this is equivalent
35+
* @example <caption>Array with numbers from 0 to 9</caption>
36+
* newArrayOf(10, i => i) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
37+
* @example <caption>Array of even numbers</caption>
38+
* newArrayOf(5, i => (i + 1) * 2) // [2, 4, 6, 8, 10]
39+
*/
1940
function newArrayOf (n, data) {
2041
const a = []
2142

0 commit comments

Comments
 (0)
Please sign in to comment.