-
Notifications
You must be signed in to change notification settings - Fork 7
/
linrand.js
33 lines (32 loc) · 862 Bytes
/
linrand.js
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
33
/**
* Fill an Array with random values in the range *minVal* to *maxVal* with a linear distribution.
* @arguments _(size [, minVal=0, maxVal=1])_
* @example
* Array.linrand(8, 1, 100);
*/
sc.define("*linrand", {
Array: function(size, minVal, maxVal) {
minVal = minVal === void 0 ? 0 : minVal;
maxVal = maxVal === void 0 ? 1 : maxVal;
var a = new Array(size|0);
var range = maxVal - minVal;
for (var i = 0, imax = a.length; i < imax; i++) {
a[i] = minVal + range.linrand();
}
return a;
}
});
/**
* @arguments _none_
* @returns a linearly distributed random number from zero to this.
* @example
* (10).linrand();
*/
sc.define("linrand", {
Number: function() {
return Math.min(Math.random(), Math.random()) * this;
},
Array: function() {
return this.map(function(x) { return x.linrand(); });
}
});